Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 4 of 4
  1. #1
    Regular Coder
    Join Date
    Nov 2002
    Posts
    672
    Thanks
    1
    Thanked 1 Time in 1 Post

    innerHTML and exact strings with indexOf problem in IE

    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:

    for(i=0;i<document.getElementsByTagName('td');i++)
    {
    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?

  • #2
    Master Coder
    Join Date
    Feb 2003
    Location
    UmeŚ, Sweden
    Posts
    5,575
    Thanks
    0
    Thanked 83 Times in 74 Posts
    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.
    liorean <[lio@wg]>
    Articles: RegEx evolt wsabstract , Named Arguments
    Useful Threads: JavaScript Docs & Refs, FAQ - HTML & CSS Docs, FAQ - XML Doc & Refs
    Moz: JavaScript DOM Interfaces MSDN: JScript DHTML KDE: KJS KHTML Opera: Standards

  • #3
    Senior Coder
    Join Date
    Jun 2002
    Location
    frankfurt, german banana republic
    Posts
    1,848
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Aside from the tagsoup issue, your initialization of the for loop contains a bug:

    Code:
    for(i=0;i<document.getElementsByTagName('td');i++)
    should rather be


    Code:
    for(i=0;i<document.getElementsByTagName('td').length;i++)
    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.
    De gustibus non est disputandum.

  • #4
    Master Coder
    Join Date
    Feb 2003
    Location
    UmeŚ, Sweden
    Posts
    5,575
    Thanks
    0
    Thanked 83 Times in 74 Posts
    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.)
    liorean <[lio@wg]>
    Articles: RegEx evolt wsabstract , Named Arguments
    Useful Threads: JavaScript Docs & Refs, FAQ - HTML & CSS Docs, FAQ - XML Doc & Refs
    Moz: JavaScript DOM Interfaces MSDN: JScript DHTML KDE: KJS KHTML Opera: Standards


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •