I’m making a bookmarklet, but I’ve encountered some wierd behaviour in IE8. The code causing the problem is this:
var els = document.getElementById("my_id").getElementsByTagName("*");
for(var i in els)
{
alert(i+","+els[i])
}
The first thing that is alerted is "length, n". This isn’t the case in chrome: just in IE8.
Interestingly, it seems to behave differently depending on whether the code goes in the console/address bar or the page itself.
Is this standard behaviour?
EDIT:
Not down to the website that I run it on either. Is it possible that getElementsByTagName returns an array with a "length" key set in IE? It certainly doesn’t return a pure array.
What you get is not an array, it is a nodeList.
See here, click the link “getElementsByTagName(‘element_name’)”
Furthermore, for..in is not meant to be used for iterating over an array anyway. Use
for array iteration.
For a nodelist, you can get the element with
if you want to do it “right”.