Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Javascript > getElementsByTagName

Reply
Thread Tools

getElementsByTagName

 
 
Michel Bany
Guest
Posts: n/a
 
      07-07-2005
I am trying to parse responseXML from an HTTP request.
var doc = request.responseXML;
var elements = doc.getElementsByTagName("*");
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.
 
Reply With Quote
 
 
 
 
knocte
Guest
Posts: n/a
 
      07-07-2005
Michel Bany escribió:
> I am trying to parse responseXML from an HTTP request.
> var doc = request.responseXML;
> var elements = doc.getElementsByTagName("*");
> 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.


I think that the function getElementsByTagName 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 getElementsByTagName_IE(sTag){
if ((!sTag)||(sTag=='')||(sTag=='*')){
return window.document.all;
}

return document.all.tags(sTag);
}
window.document.getElementsByTagName = getElementsByTagName_IE;
}

Regards,

knocte

--
 
Reply With Quote
 
 
 
 
Jim Ley
Guest
Posts: n/a
 
      07-07-2005
On Fri, 08 Jul 2005 01:27:54 +0200, knocte
<(E-Mail Removed)> wrote:

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


this is not a remotely acceptable way of detecting if the behaviour of
getElementsByTagName("*") 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.getElementsByTagName) {
var els=document.getElementsByTagName("*");
if (els.length==0) {
// do whatever...
}
}


Jim.
 
Reply With Quote
 
RobG
Guest
Posts: n/a
 
      07-08-2005
Michel Bany wrote:
> I am trying to parse responseXML from an HTTP request.
> var doc = request.responseXML;
> var elements = doc.getElementsByTagName("*");
> 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.


Something like the following should do the trick:

function getEmAll() {
var els;
if ( document.getElementsByTagName ) {
els = document.getElementsByTagName('*');
}
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
 
Reply With Quote
 
Stephen Chalmers
Guest
Posts: n/a
 
      07-08-2005
Michel Bany <(E-Mail Removed)> wrote in message news:42cd9eb6$0$1254$(E-Mail Removed)...
> I am trying to parse responseXML from an HTTP request.
> var doc = request.responseXML;
> var elements = doc.getElementsByTagName("*");
> 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.


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 getElementsByTagName wildcard.

var allElements=document.all || document.getElementsByTagName("*");

if(typeof allElements != 'undefined' && allElements.length)
...

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.getElementsByTagName("*"), probably the html element.


function getElements(allElems, 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(allElems,obj.childNodes[i])
}
}

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


--
Stephen Chalmers
547265617375726520627572696564206174204F2E532E2072 65663A205451323437393134



 
Reply With Quote
 
ASM
Guest
Posts: n/a
 
      07-08-2005
Jim Ley wrote:
>
> if (document.getElementsByTagName) {
> var els=document.getElementsByTagName("*");
> if (els.length==0) {
> // do whatever...
> }
> }


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
 
Reply With Quote
 
Martin Honnen
Guest
Posts: n/a
 
      07-08-2005


Michel Bany wrote:

> I am trying to parse responseXML from an HTTP request.
> var doc = request.responseXML;
> var elements = doc.getElementsByTagName("*");
> the last statement returns an empty collection when running from IE6.


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
getElementsByTagName("*") gives you an empty collection?


--

Martin Honnen
http://JavaScript.FAQTs.com/
 
Reply With Quote
 
Michael Winter
Guest
Posts: n/a
 
      07-08-2005
On 08/07/2005 07:28, ASM wrote:

[snip]

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


IE is deficient and doesn't recognise the hover pseudo-class on any
elements except A.

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


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.getElementsByTagName) {
collection = document.getElementsByTagName('*');
}

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

/* 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.getByTagName = useDOM;
} else {
e = useTags(a, tN);
if(e.length) {this.getByTagName = useTags;}
}
}
return e;
}
function useDOM(a, tN) {
return a.getElementsByTagName(tN);
}
function useEmpty() {return [];}
function useTags(a, tN) {
return ('*' == tN) ? a.all : a.all.tags(tN);
}
return function(a, tN) {
this.getByTagName = isGenericObject(a.getElementsByTagName)
? isObject(a.all) && isObject(a.all.tags)
? tryDOM
: useDOM
: isObject(a.all) && isObject(a.all.tags)
? useTags
: useEmpty;

return this.getByTagName(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.
 
Reply With Quote
 
ASM
Guest
Posts: n/a
 
      07-08-2005
Michael Winter wrote:
>
> IE is deficient and doesn't recognise the hover pseudo-class on any
> elements except A.


Yes, it's what I said (or approx)
and it is exactly what I want to detect.

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

>
>
> 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.getElementsByTagName) {
> collection = document.getElementsByTagName('*');
> }


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)

>
> /* If the previous attempt failed... */
> if(!collection || !collection.length) {
>
> /* 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 = [];
> }
> }


I think that test is quite egal if(document.all)
and doesn't permit to say : there is IE and IE only

> More self-contained (but much more complex):


so complex that I understand anything
does that do what I want ?

> 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.getByTagName = useDOM;
> }
> else {
> e = useTags(a, tN);
> if(e.length) {this.getByTagName = useTags;}
> }
> }
> return e;
> }
> function useDOM(a, tN) {
> return a.getElementsByTagName(tN);
> }
> function useEmpty() {return [];}
> function useTags(a, tN) {
> return ('*' == tN) ? a.all : a.all.tags(tN);
> }
> return function(a, tN) {
> this.getByTagName = isGenericObject(a.getElementsByTagName)
> ? isObject(a.all) && isObject(a.all.tags)
> ? tryDOM
> : useDOM
> : isObject(a.all) && isObject(a.all.tags)
> ? useTags
> : useEmpty;
>
> return this.getByTagName(a, tN);
> };
> })();


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



--
Stephane Moriaux et son [moins] vieux Mac
 
Reply With Quote
 
Michael Winter
Guest
Posts: n/a
 
      07-09-2005
On 09/07/2005 00:58, ASM wrote:

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

>
> Yes, it's what I said (or approx)


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.

> and it is exactly what I want to detect.


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

[snip]

>> var collection;
>>
>> /* If gEBTN is supported, attempt to use it. */
>> if(document.getElementsByTagName) {
>> collection = document.getElementsByTagName('*');
>> }

>
> Does IE Windows unsupport this collection ?


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 getElementsByTagName method at all.

> I want to detect IE and IE only


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.

> Or much better :
> I want to detect browsers
> which don't recognise pseudo-class (eventualy except on tag A)


You can't do that.

[snipped getElementsByTagName emulation]

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


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 getElementsByTagName
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="this.className='on-hover';"
onmouseout="this.className='';">...</td>

Mike

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

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
SelectNodes vs. GetElementsByTagName =?Utf-8?B?Sm9l?= ASP .Net 2 11-02-2005 04:01 PM
GetElementsByTagName: object required Ragnar Heil XML 5 04-27-2005 02:08 PM
getelementsbytagname question Danny XML 1 07-23-2004 11:10 AM
xml getElementsByTagName w/o recursion? Simon Dahlbacka Python 2 02-11-2004 02:47 PM
Question about Document.getElementsByTagName(String) in org.w3c.dom mynamehere Java 0 12-14-2003 09:28 AM



Advertisments