Random JavaScript Comment
Just throwing this out there...
Does it ever bug anyone that when you need to change an attribute of an HTML tag in JavaScript you have to figure out which of the following ways will work across all the browsers for the particular element attribute you're working with? (Does it ever bug anyone that I use run-on sentences? ;) ).
Does it ever bug anyone that when you need to change an attribute of an HTML tag in JavaScript you have to figure out which of the following ways will work across all the browsers for the particular element attribute you're working with? (Does it ever bug anyone that I use run-on sentences? ;) ).
- this.element.setAttribute("width", "100") [Generic setter method call.]
- this.element.style.display = "inline" [Direct access to attribute.]
- this.element.id = "someId" [Direct access to attribute.]
- this.element.className = "someClassName" [Direct access to attribute, but you have to append "Name" to "class".]


2 Comments:
I can't say that I ever noticed. I usually change attributes through the second method. "element.style...", which has only ever given me problems changing background images thanks to IE cache issues. Nothing a little random number in the url can't fix I guess.
Although when you say 'all' browsers, what I hear is IE and FireFox. I don't usually take the time to test in other browsers usually. I know a few people out there just love Opera and all the other oddball browsers but frankly I think those guys are idiots and I laugh at the idea that they don't get to fully enjoy a ton of great sites because of developers like me. I used to feel the same way about people who used Mozilla browsers in general until IE went so long without an update. Then I just felt cheated and switched to FireFox as my primary brower.
Anyways, that's off-topic, do you have an example of what JS a browser complains about?
Hi Collin,
No, tonight I wrote a little code that used one of the above methods. It got me thinking about the JS I was writing on my PM app and how I struggled to get it all straight (which method to use for which attribute).
I remember spending a lot of time dealing with the .className one. For the life of me I couldn't figure out why I could do:
element.id = "whatever"
but to change "class" it had to be:
element.className = "whatever"
If we only had to develop for 1 browser, JS would be sweat! It's usually pretty lightweight on the client (each browser is faster using different methods of page manipulation), but dealing with multiple runtimes is a pain!
Post a Comment
<< Home