I’ve recently had occasion to get more heavily into DOM and AJAX scripting and I have to say I’m quite enjoying it. I’ve ordered Jermey Keith’s new book on the subject but In true impatient style have got on with it a bit.
The last time I really used Javascript consistently was when we didn’t care how we used onclick attributes or cluttering our markup with frightening amounts of functions in links. It always vaguely annoyed me back then but I didn’t have the Javascript expertise to address my concerns and besides, a lot of the stuff I really wanted to do wasn’t implemented in various browsers or was too haphazard.
Javascript has undergone something of a redemption of late – and why not? As Jeremy keith and others show, its fairly easy to make Javascript unobtrusive, accessible and as an interface enhancement as oppose to something thats just ‘cool’ with no real purpose.
The MagpieRSS/AJAX parser on the home page of this site is not very well coded but it enabled me to get to terms with the basics of doing and at some point I can revisit it and concentrate on doing it well.
But already I’ve run across an amusing throwback to the dear old days of browser sniffing because of a (ahem) certain browser.
Consider the following code:
if(subLinks[i].getAttribute("class") == "ddown")
I know the double equals signs are missing. I have no idea why but they just won’t render.
Its part of a larger object but I’ve taken it out so we can look at it in isolation. Its a fairly easy piece of code. Its saying, if the value of the ‘class’ attribute of the elements I’ve isolated is ‘ddown’….then go on to do something later in the code. Simple eh?
Except life’s never like that with IE is it? Try as I might, this line of code was tripping me up in IE. It worked fine in Moz, FFox and the latest version of Opera but no joy in IE. After a search I turned up this:
When retrieving the CLASS attribute using this method, set the sAttrName to be “className”, which is the corresponding Dynamic HTML (DHTML) property.
MSDN.
So is it? Are MS doing it right and everyone else doing it wrong? This seems highly unlikely.
Anyway, I now had to use a way of setting the right value for the right client. Luckily, because I was using AJAX I already had this:
if (window.XMLHttpRequest) {
.....
}else if (window.ActiveXObject) {
......
}
to name my request object,so I modified it slightly to add:
if (window.XMLHttpRequest) {
var classFix ="class";
}else if (window.ActiveXObject) {
var classFix ="className";
}
And changed the offending line of code to:
if(subLinks[i.]getAttribute(classFix) == "ddown")
Now I’m not enough of an AJAX whiz to know if this is the right or wrong way or if this is a known issue or if I’ve missed something simpler. Please let me know if I have.
Why can’t you just access the className directly:
if (sublinks[i].className == “ddown”)
?
Mainly cos I didn’t know I could!
So you’re saying I can drop the getAttribute method and just use the proeprty directy? Even better!
Yeah, try to always reference directly else all of the dom tree has to be parsed by the engine.
How are ya mukka 😉
Pretty good cheers Mike – drop me an email matey, haven’t had a natter for awhile :o)