Re: Javascript and IE? Javascript and C#?
- From: Noah Sussman <blinkdog@xxxxxxxxx>
- Date: Sat, 06 Oct 2007 04:07:07 -0000
On Oct 5, 2:56 pm, porter <leja...@xxxxxxxxx> wrote:
So here is what I imagine to be relevant. I'm working with a page that
uses a very simple javascript function change the style of an <li>
element (display=block;). The code is simple...which is why I cant
understand my problem. This function is being called by <body
onload="fop()">
Not to be overly critical but you probably want to avoid using names
of only 3 characters in length. It makes for less typing, but it's
harder for other people (and you, once you've been away for a couple
weeks) to quickly get a sense of what you are trying to accomplish.
Also consider using "unobtrusive JavaScript," it's very easy. Just
instead of <body onload="myFunc()"> try this inside your JavaScript
include or SCRIPT tag:
window.onload = myFunc;
This keeps your markup and behavior separate, which is nice if you
want to switch to a different set of behaviors (or markup) later on.
// Javascript function
function fop()
{
var dropDown = document.getElementById("region");
var firstName = dropDown.name;
Just use dropDown.id instead of dropDown.name and your problems should
go away. The NAME attribute is only for forms, and iirc is deprecated
and should go away in future versions of the HTML spec.
// Later for testing, I tried this (see below)
<li class="operatorLI" id="ZHT" name="TESTME">
If you do use NAME, the spec says the NAME you use should be the same
as the ID. But again, that would only be for backward compatibility
on a form. You don't need or want a NAME attribute here, nor is it
valid HTML. One thing that might help you in the future is the w3's
HTML validator. Run your page through this, and get rid of all the
errors before you spend time debugging your JS. Trying to get JS to
run on invalid markup is a waste of your valuable time.
http://validator.w3.org/
Now, if for some reason you did want another identifier besides the
ID, you could use a class name, or perhaps a REL attribute. But if
you've got meta-data you want associated with an element, imo it's
cleaner to build a JavaScript data structure to use as a lookup
table. Consider using the elements' IDs as your keys. This keeps
your markup free of mysterious cruft (that will just slow down the
poor person who inherits your code later on) and is more flexible:
var lookup = {
ZHT: "TESTME",
foo: "my meta-data",
bar: ['more', 'complex', 'meta', 'data']
}
//then for a trivial example, if ZHT were the first LI element in your
document
var myLiId = document.getElementsByTagName('li')[0].id;
var myLiName = lookup[ myLiId ]; //the value of myLiName is now
'TESTME'
im actually a
flash developer and damnit, I need my trace()
then you would probably love the Firebug Firefox extension, which
actually provides trace(), along with many other useful debugging
tools, including a console that is much more convenient than using
alert().
http://www.getfirebug.com/console.html
I know that IE is a little girl
that is an insult to little girls ;)
good luck with your coding
.
- Follow-Ups:
- Re: Javascript and IE? Javascript and C#?
- From: Thomas 'PointedEars' Lahn
- Re: Javascript and IE? Javascript and C#?
- References:
- Javascript and IE? Javascript and C#?
- From: porter
- Javascript and IE? Javascript and C#?
- Prev by Date: Re: Javascript and IE? Javascript and C#?
- Next by Date: AjaxAnywhere fail to refresh zone after create popup window by calling window.open()
- Previous by thread: Re: Javascript and IE? Javascript and C#?
- Next by thread: Re: Javascript and IE? Javascript and C#?
- Index(es):
Relevant Pages
|