Computers & ProgrammingFrontend DevelopmentJavaScript

JavaScript Browser Detection

While most of the JavaScript code that you use is supported across modern browsers, some of your JavaScript code will not work for some browsers, especially the older versions.

We can use the Navigator object to assist us with this problem. The Navigator object contains information about the visitor’s browser.

By knowing specific information about the user’s browser, we can serve the user’s session using the appropriate code and information. While there is no public standard that applies to the navigator object, all major browsers support it.

The Navigator object is created by JavaScript at runtime and not by manually initiating it in the JavaScript code.

PropertyDescriptionComments
appCodeNameReturns the code name of the browser 
appNameReturns the name of the browser 
appVersionReturns the version information of the browser 
cookieEnabledReturns Boolean value whether cookies are enabled in the browser 
languageReturns the default language of the browser versionNS and Firefox only
mimeTypes[]An array of all MIME types supported by the clientNS and Firefox only
platformReturns for which platform the browser is compiled 
plugins[]Returns for which platform the browser is compiledNS and Firefox only
systemLanguageReturns the default language of the operating systemIE only
userAgentReturns the user-agent header sent by the browser to the server 
userLanguageReturns the preferred language setting of the userIE only

The Navigator object has two methods that are available for you to use.

MethodDescriptionComments
javaEnabled()Returns Boolean whether or not the browser has Java enabled 
taintEnabled()Returns Boolean whether or not the browser has data tainting enabledIE and Opera only

Example

<div id="browserInfo"></div>
<script type="text/javascript">
    var x = "<p>CodeName: " + navigator.appCodeName + "</p>";
    x += "<p>Name: " + navigator.appName + "</p>";
    x += "<p>Version: " + navigator.appVersion + "</p>";
    x += "<p>Cookies Enabled: " + navigator.cookieEnabled + "</p>";
    x += "<p>Platform: " + navigator.platform + "</p>";
    x += "<p>User-agent header: " + navigator.userAgent + "</p>";
    document.getElementById("browserInfo").innerHTML = x;
</script>

Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top