Object getElementsByTagName IE7 Bug


So here’s a fun bug to keep you entertained this weekend. Personally, this is the first purely-JavaScript bug that I’ve seen in IE7 (More due to the lack of anything changing than the quality of the code.)

Summary:
When accessing the .all or .getElementsByTagName("*") properties on the DOM representation of an <object> element, the resulting NodeSet will always be empty.

Proof of Concept:
HTML:

  1. <object>
  2.   <param name="name" value="value"/>
  3.   <param name="name2" value="value2"/>
  4.   <param name="name3" value="value3"/>
  5. </object>

JavaScript:

  1. var obj = document.getElementsByTagName("object")[0];
  2. // => <object/>
  3. obj.all
  4. // => []
  5. obj.getElementsByTagName("*")
  6. // => []
  7. obj.getElementsByTagName("param")
  8. // => [ <param/>, <param/>, <param/> ]
  9. obj.firstChild
  10. // => <param/>

Andrea has done some fantastic work in tracking this bug down and providing a fix for it.

So far, my quick-and-dirty kludge is just to replace “*” with “param” and hope that the user is actually using an object to hold params, and not some other crazy combination.

Which reminds me – has anyone ever seen an example of an <object> element containing non-param elements on a real, live, web page – somewhere in the wild? The HTML spec says that the <object> element can contain any HTML, but that sounds a little bit too crazy for my tastes.

Posted: January 20th, 2007


If you particularly enjoy my work, I appreciate donations given with Gittip.

11 Comments (Show Comments)



Comments are closed.
Comments are automatically turned off two weeks after the original post. If you have a question concerning the content of this post, please feel free to contact me.


Secrets of the JavaScript Ninja

Secrets of the JS Ninja

Secret techniques of top JavaScript programmers. Published by Manning.

Ukiyo-e Database and Search

Ukiyo-e.org

Japanese woodblock print database and search engine.


John Resig Twitter Updates

@jeresig

Infrequent, short, updates and links.