View Full Version : innerHTML and exact strings with indexOf problem in IE

01-13-2004, 12:43 AM
On my forum I'm trying to find matches for this string in the HTML (which should appear in a td tag):

<hr width="100%" size="1" class="hr">

it wouldn't return true when I did this:

if(document.getElementsByTagName('td').item(i).innerHTML.indexOf('<hr width="100%" size="1" class="hr">')>0){alert(i)}

so I alerted the innerHTML of random td tags until I found what I was looking for...very different from what it actualy was:

<HR class=hr width="100%" SIZE=1>

Why did it rearrange the order of attributes? Why is the tag and some attributes capital? (goes against W3 standards for XHTML)
Why are some attributres unquoted? (also against W3 standards)
Is IE the only browser doing this? I don't see any logic behind it, and it would be a pain to make a script for browsers that do it the right way and then make a script to do it the wrong way. Is there some mode IE has to turn off this oddness?

01-13-2004, 12:50 AM
Welcome to the wonderful world of MSHTML, the tagsoup parser/handler for iew. Iew doesn't really know about XHTML, it only knows about XML and HTML. So, your documents are going through the tagsoup parser in any case, and the tagsoup parser works through the internal document tree ot iew. The uppercase is because of the DOM. The order should be alphabetical in one of ie5.5w and ie6w, and random in the other, but I can't remember which one does it how. Iew only uses quotes for non-alphanumeric attributes, even though only numeric attributes should do that in SGML (IIRC, but I may be wrong about this). Your best chance is to do a comparison from the DOM, getElementByTagName('hr'), getAttribute('width') == '100%' etc.

01-13-2004, 01:20 AM
Aside from the tagsoup issue, your initialization of the for loop contains a bug:


should rather be


because you need the length of the node list. I would however store the result of getElementsByTagName() in a temporary variable and use that in the loop, simplifies typing.

01-13-2004, 01:25 AM
You should never use function calls or properties of an object in the condition of a loop if you can reduce them to a stable variable that won't have it's value changed inside the loop. (There's an exception, but I won't get into that. The general rule is more important.)