articles

Home / DeveloperSection / Articles / JavaScript-Browser Compatibility

JavaScript-Browser Compatibility

Danish Khan6899 19-Nov-2012
Introduction:

There are lots of browsers which are there in the market; we must know the differences between

those browsers. Different browsers support different tags so while designing the web page we must

know which browsers support which tags. We must know in which browser our web page is running.

To know on which browser our web page is running we will use the built-in Navigator object.

Navigator Properties:

Properties

Description

 

appCodeName

It returns the code name of the browser.

appName

It returns the name of the browser.

appVersion

It returns the version information of the browser

cookieEnabled

It determines whether cookies are enabled in the browser.

onLine

It  returns true if the browser is online otherwise false.

platform

It is a string. It returns for which platform the browser was compiled.

userAgent

It contains the code name and version of the browser. This value is sent to the originating server to identify the client.

 

Navigator Object Methods:

Method

Description

javaEnabled()

It specifies whether Java is enabled or not.

taintEnabled()

It will return true or false based on whether Browser has data tainting enabled or not.

  

<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    <script type="text/javascript">
        function findBrowse() {
            var userAgent = navigator.userAgent;
            var ie = (userAgent.indexOf('MSIE') != -1);
            var netscape = (userAgent.indexOf('Mozilla') != -1);
            var opera = (userAgent.indexOf('Opera') != -1);
            var version = navigator.appVersion;
            if (opera) {
                document.write("Opera code will execute");
            } else if (ie) {
                document.write("Internet Explorer based coding will execute");
            } else if (netscape) {
                document.write("Netscape Navigator coding will execute");
            }
            else {
                document.write("Other Browser");
            }
        }
    </script>
</head>
<body>
    <div>
        <input id="btnFindBrowser" type="button" onclick="findBrowse()" value="Find Browser" />
    </div>
</body>
</html>

If we run this code on Internet explorer then only the code block of Internet Explorer will execute.

This code is beneficial to when there are compatibility issues. 

 Example of Navigator Object Properties:   

<html xmlns="http://www.w3.org/1999/xhtml">

<head>
</head>
<body>
    <script type="text/javascript">
        var appname = navigator.appName;
        document.write("Application Name : " + appname + "<br/>");
        var appversion = navigator.appVersion;
        document.write("Application version : " + appversion + "<br/>");
        var platform = navigator.platform;
        document.write("Platform for which the browser was compiled : " + platform +  
        "<br/>");
        var cookieEnabled = navigator.cookieEnabled
        if (cookieEnabled) {
            document.write("Cookie Enabled <br/>");
        }
        else {
            document.write("Cookie not enabled");
        }
    </script>
</body>
</html>

  Output : 

JavaScript-Browser Compatibility

In the above example we have find the Application name which is the browser name, Application Version i.e.

 its version information, Platform for which the browser was compiled and whether the cookie is enabled or not.

Conclusion:

In this article I explained about Navigator Object and what is the purpose of the Navigator Object.



Updated 07-Sep-2019

Leave Comment

Comments

Liked By