views:

463

answers:

3

document.getElementsByTagName("*") It works for IE/FF/Opera, But doesn't work for Chrom and Safari

document.all works for IE/Chrom/Safari, But doesn't work for FF.

How can I deal with it?

Many thanks

A: 

I admit that there might be technologies today I don't know about that allow for this sort of thing to be done in cross-browser format, but the way I've always had to do it in the past is to have a check of some sort for which browser you're using.

A simpler solution, though, is to try to run one of them, and if you get nothing/null/an error, use the other.

Anyway, if you really don't want to deal with it yourself, you should use a library that will deal with it for you (e.g., jQuery).

Platinum Azure
+2  A: 

Try like this:

if (document.all)
{
   allElements = document.all;
}
else
{
   allElements = document.getElementsByTagName("*");
}

Or shorter version

allElements = document.all ? document.all : document.getElementsByTagName("*");
Balon
Thanks , but still doesn't work for Chrom and Safari
Jason
I've just checked and it works on Chrome and I bet that it works on Safari too.
Balon
solved it without using either of these
Jason
A: 

document.getElementsByTagName() works perfectly in all modern browsers (everything newer than IE5).

If it doesn't seem to work in Chrome or Safari, then it's most likely just a symptom of an error you have elsewhere.

porneL