getElementsByTagName

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Michel Bany

    getElementsByTagName

    I am trying to parse responseXML from an HTTP request.
    var doc = request.respons eXML;
    var elements = doc.getElements ByTagName("*");
    the last statement returns an empty collection when running from IE6.
    It returns the expected collection when running under Firefox or Mozilla.
    Anybody can explain ?
    Michel.
  • knocte

    #2
    Re: getElementsByTa gName

    Michel Bany escribió:[color=blue]
    > I am trying to parse responseXML from an HTTP request.
    > var doc = request.respons eXML;
    > var elements = doc.getElements ByTagName("*");
    > the last statement returns an empty collection when running from IE6.
    > It returns the expected collection when running under Firefox or Mozilla.
    > Anybody can explain ?
    > Michel.[/color]

    I think that the function getElementsByTa gName does not accept the
    string "*" in IE. You will have to correct the DOM behaviour of IE with
    something like this:

    function IsIE(){
    return window.document .all?true:false ;
    }

    if (IsIE()) {
    function getElementsByTa gName_IE(sTag){
    if ((!sTag)||(sTag =='')||(sTag==' *')){
    return window.document .all;
    }

    return document.all.ta gs(sTag);
    }
    window.document .getElementsByT agName = getElementsByTa gName_IE;
    }

    Regards,

    knocte

    --

    Comment

    • Jim Ley

      #3
      Re: getElementsByTa gName

      On Fri, 08 Jul 2005 01:27:54 +0200, knocte
      <knocte@NO-SPAM-PLEASE-gmail.com> wrote:
      [color=blue]
      >function IsIE(){
      > return window.document .all?true:false ;
      >}[/color]

      this is not a remotely acceptable way of detecting if the behaviour of
      getElementsByTa gName("*") returns what you want it to, document.all is
      supported by a very large number of user agents, and is certainly not
      a discriminator for IE!

      if (document.getEl ementsByTagName ) {
      var els=document.ge tElementsByTagN ame("*");
      if (els.length==0) {
      // do whatever...
      }
      }


      Jim.

      Comment

      • RobG

        #4
        Re: getElementsByTa gName

        Michel Bany wrote:[color=blue]
        > I am trying to parse responseXML from an HTTP request.
        > var doc = request.respons eXML;
        > var elements = doc.getElements ByTagName("*");
        > the last statement returns an empty collection when running from IE6.
        > It returns the expected collection when running under Firefox or Mozilla.
        > Anybody can explain ?
        > Michel.[/color]

        Something like the following should do the trick:

        function getEmAll() {
        var els;
        if ( document.getEle mentsByTagName ) {
        els = document.getEle mentsByTagName( '*');
        }
        if ( ( !els || !els.length ) && document.all ) {
        els = document.all;
        }
        return els;
        }

        Note you should check the returned value, there is still plenty of scope
        for failure.

        --
        Rob

        Comment

        • Stephen Chalmers

          #5
          Re: getElementsByTa gName

          Michel Bany <m.bany@wanadoo x.fr> wrote in message news:42cd9eb6$0 $1254$8fcfb975@ news.wanadoo.fr ...[color=blue]
          > I am trying to parse responseXML from an HTTP request.
          > var doc = request.respons eXML;
          > var elements = doc.getElements ByTagName("*");
          > the last statement returns an empty collection when running from IE6.
          > It returns the expected collection when running under Firefox or Mozilla.
          > Anybody can explain ?
          > Michel.[/color]

          I get around this by giving priority to document.all if it is available. This will support I.E. but may not work for
          some DOM browsers that do not support document.all or the getElementsByTa gName wildcard.

          var allElements=doc ument.all || document.getEle mentsByTagName( "*");

          if(typeof allElements != 'undefined' && allElements.len gth)
          ...

          This page http://www.siteexperts.com/tips/contents/ts16/page3.asp shows a recursive function that accesses all
          elements. Modifying it to append to a supplied array, I found that it returns one element less than
          document.getEle mentsByTagName( "*"), probably the html element.


          function getElements(all Elems, obj)
          {
          for (var i=0;i < obj.childNodes. length;i++)
          if (obj.childNodes[i].nodeType==1) // Elements only


          allElems[allElems.length]=obj.childNodes[i];
          getElements(all Elems,obj.child Nodes[i])
          }
          }

          var allElements=[];
          getElements( allElements, document.childN odes[0])


          --
          Stephen Chalmers
          547265617375726 520627572696564 206174204F2E532 E207265663A2054 51323437393134



          Comment

          • ASM

            #6
            Re: getElementsByTa gName

            Jim Ley wrote:[color=blue]
            >
            > if (document.getEl ementsByTagName ) {
            > var els=document.ge tElementsByTagN ame("*");
            > if (els.length==0) {
            > // do whatever...
            > }
            > }[/color]

            hello Jim,

            IE is not reactive with this ccs rule :
            td { color: blue }
            td:hover { color: red }

            if(document.all ) being digested by others browsers than IE
            which detection have I to place in my code in this case ?


            --
            Stephane Moriaux et son [moins] vieux Mac

            Comment

            • Martin Honnen

              #7
              Re: getElementsByTa gName



              Michel Bany wrote:
              [color=blue]
              > I am trying to parse responseXML from an HTTP request.
              > var doc = request.respons eXML;
              > var elements = doc.getElements ByTagName("*");
              > the last statement returns an empty collection when running from IE6.[/color]

              The wild card '*' is supported when I test here with MSXML 3 on Windows
              XP and IE 6, I am not sure about earlier MSXML releases without testing.

              Are you sure the XML is properly loaded and parsed when
              getElementsByTa gName("*") gives you an empty collection?


              --

              Martin Honnen

              Comment

              • Michael Winter

                #8
                Re: getElementsByTa gName

                On 08/07/2005 07:28, ASM wrote:

                [snip]
                [color=blue]
                > IE is not reactive with this ccs rule :
                > td { color: blue }
                > td:hover { color: red }[/color]

                IE is deficient and doesn't recognise the hover pseudo-class on any
                elements except A.
                [color=blue]
                > if(document.all ) being digested by others browsers than IE
                > which detection have I to place in my code in this case ?[/color]

                You don't perform any detection at all, at least in the sense of looking
                for IE. What you're looking for is specific behaviour, and then an
                alternative method of achieving what you want.

                var collection;

                /* If gEBTN is supported, attempt to use it. */
                if(document.get ElementsByTagNa me) {
                collection = document.getEle mentsByTagName( '*');
                }

                /* If the previous attempt failed... */
                if(!collection || !collection.len gth) {

                /* Check to see if we can use the all property
                * to obtain a collection of all elements in
                * the document.
                *
                * Note that this is *not* a check for IE, but
                * a feature test.
                */
                if(document.all ) {
                collection = document.all;

                /* If not, return an empty array as failure. */
                } else {
                collection = [];
                }
                }


                More self-contained (but much more complex):

                var getByTagName = (function() {
                function isGenericObject (value) {
                return isObject(value) || ('function' == typeof value);
                }
                function isObject(value) {
                return !!value && ('object' == typeof value);
                }
                function tryDOM(a, tN) {
                var e = useDOM(a, tN);

                if('*' == tN) {
                if(e.length) {
                this.getByTagNa me = useDOM;
                } else {
                e = useTags(a, tN);
                if(e.length) {this.getByTagN ame = useTags;}
                }
                }
                return e;
                }
                function useDOM(a, tN) {
                return a.getElementsBy TagName(tN);
                }
                function useEmpty() {return [];}
                function useTags(a, tN) {
                return ('*' == tN) ? a.all : a.all.tags(tN);
                }
                return function(a, tN) {
                this.getByTagNa me = isGenericObject (a.getElementsB yTagName)
                ? isObject(a.all) && isObject(a.all. tags)
                ? tryDOM
                : useDOM
                : isObject(a.all) && isObject(a.all. tags)
                ? useTags
                : useEmpty;

                return this.getByTagNa me(a, tN);
                };
                })();

                What you'd use depends on whether you'd end up repeating code. The above
                really boils down to something quite simple and efficient, though it may
                not look like it at first glance.

                Mike

                --
                Michael Winter
                Prefix subject with [News] before replying by e-mail.

                Comment

                • ASM

                  #9
                  Re: Detect IE [was] getElementsByTa gName

                  Michael Winter wrote:[color=blue]
                  >
                  > IE is deficient and doesn't recognise the hover pseudo-class on any
                  > elements except A.[/color]

                  Yes, it's what I said (or approx)
                  and it is exactly what I want to detect.
                  [color=blue][color=green]
                  >> if(document.all ) being digested by others browsers than IE
                  >> which detection have I to place in my code in this case ?[/color]
                  >
                  >
                  > You don't perform any detection at all, at least in the sense of looking
                  > for IE. What you're looking for is specific behaviour, and then an
                  > alternative method of achieving what you want.
                  >
                  > var collection;
                  >
                  > /* If gEBTN is supported, attempt to use it. */
                  > if(document.get ElementsByTagNa me) {
                  > collection = document.getEle mentsByTagName( '*');
                  > }[/color]

                  Does IE Windows unsupport this collection ?

                  I want to detect IE and IE only
                  Or much better :
                  I want to detect browsers
                  which don't recognise pseudo-class (eventualy except on tag A)
                  [color=blue]
                  >
                  > /* If the previous attempt failed... */
                  > if(!collection || !collection.len gth) {
                  >
                  > /* Check to see if we can use the all property
                  > * to obtain a collection of all elements in
                  > * the document.
                  > *
                  > * Note that this is *not* a check for IE, but
                  > * a feature test.
                  > */
                  > if(document.all ) {
                  > collection = document.all;
                  >
                  > /* If not, return an empty array as failure. */
                  > } else {
                  > collection = [];
                  > }
                  > }[/color]

                  I think that test is quite egal if(document.all )
                  and doesn't permit to say : there is IE and IE only
                  [color=blue]
                  > More self-contained (but much more complex):[/color]

                  so complex that I understand anything
                  does that do what I want ?
                  [color=blue]
                  > var getByTagName = (function() {
                  > function isGenericObject (value) {
                  > return isObject(value) || ('function' == typeof value);
                  > }
                  > function isObject(value) {
                  > return !!value && ('object' == typeof value);
                  > }
                  > function tryDOM(a, tN) {
                  > var e = useDOM(a, tN);
                  > if('*' == tN) {
                  > if(e.length) {
                  > this.getByTagNa me = useDOM;
                  > }
                  > else {
                  > e = useTags(a, tN);
                  > if(e.length) {this.getByTagN ame = useTags;}
                  > }
                  > }
                  > return e;
                  > }
                  > function useDOM(a, tN) {
                  > return a.getElementsBy TagName(tN);
                  > }
                  > function useEmpty() {return [];}
                  > function useTags(a, tN) {
                  > return ('*' == tN) ? a.all : a.all.tags(tN);
                  > }
                  > return function(a, tN) {
                  > this.getByTagNa me = isGenericObject (a.getElementsB yTagName)
                  > ? isObject(a.all) && isObject(a.all. tags)
                  > ? tryDOM
                  > : useDOM
                  > : isObject(a.all) && isObject(a.all. tags)
                  > ? useTags
                  > : useEmpty;
                  >
                  > return this.getByTagNa me(a, tN);
                  > };
                  > })();[/color]

                  As I understood anything, does this getByTagName return false if
                  pseudo-class not allowed for tags TD ?



                  --
                  Stephane Moriaux et son [moins] vieux Mac

                  Comment

                  • Michael Winter

                    #10
                    Re: Detect IE [was] getElementsByTa gName

                    On 09/07/2005 00:58, ASM wrote:
                    [color=blue]
                    > Michael Winter wrote:
                    >[color=green]
                    >> IE is deficient and doesn't recognise the hover pseudo-class on any
                    >> elements except A.[/color]
                    >
                    > Yes, it's what I said (or approx)[/color]

                    Your comment was, to me, so random and at a tangent to the current topic
                    that I read it as a question, rather than a statement.
                    [color=blue]
                    > and it is exactly what I want to detect.[/color]

                    That's the first time you've said as much.

                    [snip]
                    [color=blue][color=green]
                    >> var collection;
                    >>
                    >> /* If gEBTN is supported, attempt to use it. */
                    >> if(document.get ElementsByTagNa me) {
                    >> collection = document.getEle mentsByTagName( '*');
                    >> }[/color]
                    >
                    > Does IE Windows unsupport this collection ?[/color]

                    Again, I'm confused as to what you mean. Perhaps: "Will IE/Win ever
                    return a non-empty collection using this code?" If so, then yes, IE6
                    will return a collection containing all elements within the document, as
                    it should. IE5.x will always return an empty collection, and IE4 doesn't
                    support the getElementsByTa gName method at all.
                    [color=blue]
                    > I want to detect IE and IE only[/color]

                    Browser detection is flawed, and has been since browsers first started
                    trying to spoof other browsers. However, you can use Microsoft's
                    conditional comments mechanism which is, as far as I know, only
                    implemented by IE.
                    [color=blue]
                    > Or much better :
                    > I want to detect browsers
                    > which don't recognise pseudo-class (eventualy except on tag A)[/color]

                    You can't do that.

                    [snipped getElementsByTa gName emulation]
                    [color=blue]
                    > As I understood anything, does this getByTagName return false if
                    > pseudo-class not allowed for tags TD ?[/color]

                    You've only just explained your desire to detect the lack of support for
                    the :hover pseudo-class, so of course that code doesn't do what you
                    want. It accomplished what I read as the direction of discussion: how to
                    handle earlier IE versions which didn't support the getElementsByTa gName
                    method properly.


                    You cannot detect what is supported in CSS via scripting. However, what
                    you can do is emulate some of that functionality. If you want to alter
                    the appearance of an element, then implement it in both CSS and script:

                    td {
                    background-color: transparent;
                    color: blue;
                    }

                    td.on-hover,
                    td:hover {
                    background-color: transparent;
                    color: red;
                    }


                    <td onmouseover="th is.className='o n-hover';"
                    onmouseout="thi s.className=''; ">...</td>

                    Mike

                    --
                    Michael Winter
                    Prefix subject with [News] before replying by e-mail.

                    Comment

                    • Michel Bany

                      #11
                      Re: getElementsByTa gName

                      [color=blue]
                      >This page http://www.siteexperts.com/tips/contents/ts16/page3.asp shows a recursive function that accesses all
                      >elements. Modifying it to append to a supplied array, I found that it returns one element less than
                      >document.getEl ementsByTagName ("*"), probably the html element.
                      >
                      >
                      >function getElements(all Elems, obj)
                      >{
                      > for (var i=0;i < obj.childNodes. length;i++)
                      > if (obj.childNodes[i].nodeType==1) // Elements only
                      >
                      >
                      > allElems[allElems.length]=obj.childNodes[i];
                      > getElements(all Elems,obj.child Nodes[i])
                      > }
                      >}
                      >
                      >var allElements=[];
                      >getElements( allElements, document.childN odes[0])
                      >
                      >
                      >[/color]
                      This recursive function works superbly with Firefox and Mozilla, but not
                      with IE6,
                      var request = new ActiveXObject(" Microsoft.XMLHT TP");
                      <.. build and send the request ..>
                      var nodes = request.respons eXML.childNodes[0];
                      I am getting an empty collection for nodes. What's wrong ?
                      Michel.

                      Comment

                      • ASM

                        #12
                        Re: Detect IE [was] getElementsByTa gName

                        Michael Winter wrote:

                        sory to have turned of a file from its original question
                        as mine would be seen as an extension ( ?)

                        thanks for your patience and answer
                        [color=blue]
                        > On 09/07/2005 00:58, ASM wrote:
                        >[color=green]
                        >> I want to detect browsers
                        >> which don't recognise pseudo-class (eventualy except on tag A)[/color]
                        >
                        > You can't do that.[/color]

                        [snip]

                        [color=blue]
                        > You cannot detect what is supported in CSS via scripting. However, what
                        > you can do is emulate some of that functionality. If you want to alter
                        > the appearance of an element, then implement it in both CSS and script:
                        >
                        > td {
                        > background-color: transparent;
                        > color: blue;
                        > }
                        >
                        > td.on-hover,
                        > td:hover {
                        > background-color: transparent;
                        > color: red;
                        > }
                        >
                        >
                        > <td onmouseover="th is.className='o n-hover';"
                        > onmouseout="thi s.className=''; ">...</td>[/color]

                        because I wouln't want charge css compatible browsers with events they
                        don't need (and aso not to much poluate html code)
                        and because I have double class (and theire hovers) on same element
                        (depend if selected or not)
                        by that my wanted is some more complex than a simple roll-over
                        I'd made a JS code attibutting at page loaded this events only if IE

                        exercices (tables rows or cells selections)
                        examples are (in french) here :

                        more complex :

                        to compare with same for only css (and few JS)


                        all of them based on an ie dectection (mixed with iCab)
                        detection ignoring capabilities of other unknown browsers

                        --
                        Stephane Moriaux et son [moins] vieux Mac

                        Comment

                        • Martin Honnen

                          #13
                          Re: getElementsByTa gName



                          Michel Bany wrote:

                          [color=blue]
                          > var request = new ActiveXObject(" Microsoft.XMLHT TP");
                          > <.. build and send the request ..>
                          > var nodes = request.respons eXML.childNodes[0];
                          > I am getting an empty collection for nodes. What's wrong ?[/color]


                          Hard to tell, check
                          request.status
                          request.statusT ext
                          request.getAllR esponseHEaders( )
                          then check
                          request.respons eXML.parseError .errorCode
                          request.respons eXML.parseError .reason
                          for IE, that should allow to tell what happens.



                          --

                          Martin Honnen

                          Comment

                          • VK

                            #14
                            Re: getElementsByTa gName

                            > I am trying to parse responseXML from an HTTP request.[color=blue]
                            > var doc = request.respons eXML;
                            > var elements = doc.getElements ByTagName("*");[/color]

                            responseXML doesn't work this way in IE. Overall it's a bogus method:
                            doesn't fail on calling it, but always gives you empty string for any
                            XML methods. If you *really* have a valid XML data from your server,
                            you have to use XML parser first:

                            ....
                            if (window.XMLHttp Request) {
                            // FF & Co
                            }
                            else {
                            try {
                            var myDataIsland = new ActiveXObject(" Msxml3.DOMDocum ­ent");}
                            catch (e1) {
                            var myDataIsland = new ActiveXObject(" Msxml2.DOMDocum ­ent");}
                            }
                            if (myDataIsland != null) {
                            var myInOutStream = new ActiveXObject(" Microsoft.XMLHT ­TP");
                            <pseudo-code - read data using myInOutStream>
                            <pseudo-code - if success>
                            myDataIsland.lo adXML(myInOutSt ­ream.responseT ext);
                            }
                            }
                            [color=blue]
                            >From this point forward myDataIsland contains perfectly formatted and[/color]
                            parsed XML data.

                            Now you can: var elm = myDataIsland.ge tElementsByTagN ame("*");
                            It works perfectly in IE.

                            All the above has sense only if you are really getting a valid XML from
                            your server. If it's just an unstructured set of strings you're calling
                            XML for convenience, then simply read your text and parse it manually
                            in the way you want:
                            var txt = myInOutSt­ream. responseText;
                            ....

                            responseText method always works no matter what.

                            Comment

                            • ASM

                              #15
                              Re: getElementsByTa gName

                              VK wrote:[color=blue][color=green]
                              >>I am trying to parse responseXML from an HTTP request.
                              >>var doc = request.respons eXML;
                              >>var elements = doc.getElements ByTagName("*");[/color]
                              >
                              >
                              > responseXML doesn't work this way in IE. Overall it's a bogus method:
                              > doesn't fail on calling it, but always gives you empty string for any
                              > XML methods. If you *really* have a valid XML data from your server,
                              > you have to use XML parser first:
                              >
                              > ...
                              > if (window.XMLHttp Request) {
                              > // FF & Co
                              > }
                              > else {[/color]

                              if(document.all && navigator.userA gent.indexOf('M ac')<0)
                              [color=blue]
                              > try {[/color]

                              try is not understood by my NC 4.5 and gives an error
                              [color=blue]
                              > var myDataIsland = new ActiveXObject(" Msxml3.DOMDocum ­ent");}
                              > catch (e1) {
                              > var myDataIsland = new ActiveXObject(" Msxml2.DOMDocum ­ent");}
                              > }
                              > if (myDataIsland != null) {
                              > var myInOutStream = new ActiveXObject(" Microsoft.XMLHT ­TP");
                              > <pseudo-code - read data using myInOutStream>
                              > <pseudo-code - if success>
                              > myDataIsland.lo adXML(myInOutSt ­ream.responseT ext);
                              > }
                              > }
                              >[color=green]
                              >>From this point forward myDataIsland contains perfectly formatted and[/color]
                              > parsed XML data.
                              >
                              > Now you can: var elm = myDataIsland.ge tElementsByTagN ame("*");
                              > It works perfectly in IE.
                              >
                              > All the above has sense only if you are really getting a valid XML from
                              > your server. If it's just an unstructured set of strings you're calling
                              > XML for convenience, then simply read your text and parse it manually
                              > in the way you want:
                              > var txt = myInOutSt­ream. responseText;
                              > ...
                              >
                              > responseText method always works no matter what.
                              >[/color]


                              --
                              Stephane Moriaux et son [moins] vieux Mac

                              Comment

                              Working...