getElementsByTagName() behaves differently in IE and FF
I am new to JavaScript, trying to figure out a tag information value. GWT Code is as follows..
public static native boolean isToolBarInstalled() /*-{
alert("Validating the toolbar installed.");
var metas = document.getElementsByTagName('head')[0].getElementsByTagName('meta');
var i;
alert ("Meta length: "+metas.length);
for (i = 0; i < metas.length; i++){
alert("Value: "+metas[i].value);
if (metas[i].getAttribute('name') == "toolbar"){
return true;
}
}
return false;
}-*/;
FF return true whereas IE returns false, for the same page? Any clue/suggestions would be helpful.
WM.
HTML is too huge to post, here is a snippet of the code..
<html>
<head>
开发者_开发百科 ....
<title>My App</title>
<meta name="toolbar" content="1.0">
</head>
<body>
.....
</body>
<html>
Works for me in IE7.
Check you haven't got any text content before the <meta>
end-tag other than simple whitespace.
If you have any non-whitespace text in or before <html>
or <head>
, the browser will decide that you meant to open the <body>
to contain the text. (This is actually valid in non-XHTML HTML, as the </head>
end-tag and the <body>
start-tag are optional.) That means closing the <head>
section, so the number of <meta>
tags inside <head>
will be 0.
In any case you might as well say just:
var metas= document.getElementsByTagName('meta');
as the bit about checking they're in <head>
is redundant for a valid document; that's the only place <meta>
is allowed to appear.
alert("Value: "+metas[i].value);
There's no .value
on <meta>
, do you mean .content
?
if (metas[i].getAttribute('name') == "toolbar"){
Use metas[i].name
. There's no reason to use getAttribute
/setAttribute
on an HTML document and there are problems with it on IE.
Try this:
element.attributes[value].nodeAttribute;
As explained here:
The getAttribute() method will return "null" in Internet Explorer when trying to get the for attribute of a label element.
Might be the same issue...
Well look:
- Example
- Source
In this example it is working on IE.
function isToolBarInstalled() {
alert("Validating the toolbar installed.");
var metas = document.getElementsByTagName('head')[0].getElementsByTagName('meta');
var i;
alert ("Meta length: "+metas.length);
for (i = 0; i < metas.length; i++){
alert("Value: "+metas[i].value);
var attr = metas[i].getAttribute('name');
// IE workaround
if (attr == null)
{
attr = metas[i].attributes["name"];
if (attr != null) attr = attr.nodeValue;
}
if (attr == "toolbar")
return true;
}
return false;
}
alert( "Is installed: " + isToolBarInstalled() );
精彩评论