JavaScript: Detecting Browser; Browser Sniffing

, , …,
Want to master JavaScript in a week? Buy Xah JavaScript Tutorial.

This pages shows you how to use JavaScript to find various info about browser. For example, whether cookies is on, java is installed, or about browser version.

to see info about your browser.

Here's the code.

<p><button id="button52202">Click Me</button> to see info about your browser.</p>

<div id="showinfo55223"></div>
function showBrowserInfo() {

    // create node
    var newNode = document.createElement("ul");

    var infoList = [ "<b>appName:</b> " + navigator.appName,
                     "<b>appVersion:</b> " + navigator.appVersion,
                     "<b>Platform:</b> " + navigator.platform,
                     "<b>appCodeName:</b> " + navigator.appCodeName,
                     "<b>userAgent:</b> " + navigator.userAgent,
                     "<b>cookieEnabled:</b> " + navigator.cookieEnabled,
                     "<b>javaEnabled:</b> " + navigator.javaEnabled()
                   ];

    for (var i = 0; i < infoList.length; i++) {
        var liNode = document.createElement("li");
        liNode.innerHTML=infoList[i];
        newNode.appendChild(liNode);

    }

    // insert the new node into document
    var anchor = document.getElementById("showinfo55223");
    if ( anchor.hasChildNodes() ) {
        anchor.removeChild(anchor.lastChild)
    }
    anchor.appendChild(newNode);

}

document.getElementById("button52202").addEventListener("click", showBrowserInfo, false);

navigator.cookieEnabled returns a Boolean type. The value is auto converted to string when in string context.

javaEnabled() is a method, not property. It returns a Boolean type. It is also auto converted to string here.

Reference

blog comments powered by Disqus