views:

57

answers:

2

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>
A: 

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() );
BrunoLM
Not really, the code doesn't reach getAttribute(). The line - alert ("Meta length: "+metas.length); prints 0 and the 'for' loop never gets executed.
Win Man
@Win: Maybe it's a wrong (not closed) tag? On the example it is working. Maybe it's something on your HTML.
BrunoLM
A: 

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.

bobince