document.getElementsByTagName(\"*\")
works for IE/Firefox/Opera, But doesn\'t work for Chrome and Safari.
document.all
works for IE/Chrom/S
Without sufficient rep to comment on @Khurram Hassan 's answer, I'll put it here, along with my answer to the original question.
First, the original question. My solution would be (and is in my own code, pending an answer to a problem with it which I put up on this site) document.getElementsByTagName("*"), which actually does get every single element on Chrome. I tested it out on google.com on Google Chrome with a profile loaded and the eight most common visited sites listed, and it came out to 356 individual elements with a tag name. In fairness, this included html, head, body, and others that probably aren't useful, but it still got them. I don't have access to Opera at the moment, but with Chrome still accepting that piece of JavaScript, I don't see a reason why it wouldn't accept that in your code.
Second, for @Khurram hassan, document.getElementById()
can't be used in this case. I just tested it on Chrome, and it came up with a value of null. In theory, anything with the general form of getElementsBy*
as opposed to getElementBy*
could probably be used in this case. So, to add to my earlier answer, you might also try ClassName
, Name
, and TagNameNS
depending on what you're trying to do. On the same page as before, I tested those three and while only TagNameNS
worked, the rest just returned empty lists, not errors.
Further, if it is true that you don't need <!DOCTYPE html>
in your code, perhaps you could post the working code as an edit to your answer so that we can see it. <!DOCTYPE html>
seems, to the best of my knowledge, to be the commonly accepted (and usually assumed to be mandatory) way of starting an HTML page. If it's not necessary, then that would be new information (at least to me) that could prove useful in either debugging or non-public webpages down the road.
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).
While I would not encourage you to do a document.all because it implies you are doing a lot of client-side parsing that is just not needed I do understand there is a lot of legacy stuff out there etc. I wanted to post a little extension of my thoughts on a wrapper method for document.all.
document.all = document.all || function(){
return document.getElementsByTagName("*");
};
Of course this assumes you have a getElementsByTagName function, which should be the case.
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.
Try like this:
if (document.all !== undefined)
{
allElements = document.all;
}
else
{
allElements = document.getElementsByTagName("*");
}
Or shorter version
allElements = document.all ? document.all : document.getElementsByTagName("*");
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.