document.getElementsByTagName("*") Or document.all

23,970

Solution 1

document.all should be avoided as it is not Standards compliant. Instead you can use document.getElementById() for Particular Node or use $("*") For selecting all the elements using jQuery.

But still if you would like to use document.all then see to it that <!DOCTYPE> Tag is removed from your page, as well as the xmlns attribute is also removed from your <html> tag.

Change anything like this:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">

to:

<html>

I have tested it on FireFox 19.0.2 and document.all works fine for me.

The reason: When you use the <!DOCTYPE> tag you are telling the browser that your webpage is complaint with the standards which tells you to NOT use the document.all in your script, so the browser also does not allow it.

But as you want to use it, you are obviously not following the standards so don't even bother to add the <!DOCTYPE> tag otherwise document.all won't work.

Solution 2

Try like this:

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

Or shorter version

allElements = document.all ? document.all : document.getElementsByTagName("*");

Solution 3

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.

Share:
23,970
Jason
Author by

Jason

Updated on July 09, 2022

Comments

  • Jason
    Jason almost 2 years

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

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

    How can I deal with it?