简体   繁体   中英

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>

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:

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() );

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM