Web Designing MCQs

How to Detect Visitor’s Browser Using JavaScript?

Pinterest LinkedIn Tumblr

You can detect which and what version of browser have used by your website visitors by using simple JavaScript codes. The navigator object in JavaScript contains information about the visitor’s browser. It includes information about the visitor’s browser name, browser version and more. It can be very useful to detect the visitor’s browser type and version and then serve up the appropriate information. By using this feature in JavaScript, it is easier to make your web pages smart enough to look one way to some browsers and another way to other browsers. But it may not be work for some browsers, especially for older browsers, which do not support JavaScript.

Here are some steps and codes for detecting visitor’s browser using JavaScript, it may help you to apply this feature in your website.

Read Also: How to Create JavaScript Image Slideshow with Links

How to Detect Visitor’s Browser Using JavaScript

Following is the sample JavaScript code which will detect your website visitor’s browser along with the browser version.

<script type="text/javascript">
var x=navigator
document.write("Name=" + x.appName)
document.write("<br/>")
document.write("Version=" + x.appVersion)
document.write("<br/>")
document.write("CookieEnabled=" + x.cookieEnabled)
document.write("<br/>")
document.write("BrowserLanguage=" + x.browserLanguage)
document.write("<br/>")
document.write("SystemLanguage=" + x.systemLanguage)
document.write("<br/>")
document.write("UserLanguage=" + x.userLanguage)
</script>

The different terms used on the JavaScript code defines the following.

  • appName – It holds the application name of the browser.
  • appVersion – It holds the version of the application and other things.
  • cookieEnabled – It will check whether the cookie is enabled.
  • browserLanguage – It holds the browser Language.
  • systemLanguage – It holds the system Language.
  • userLanguage – It holds the user Language.

Preview:

Following is the preview of the JavaScript code given above. It shows the browser name along with its version. You will know the browser language, system language, and user’s language. It will also show whether the cookie was enabled or not on the browser.

Read Also: How to Create a Digital Clock in JavaScript?

How to alert the user for older browser

You can alert or inform the visitor of your website by using the navigator object in JavaScript like the following. In the code given below, the browsers below version 4 are treated as the older browser and display the message. You can also add the links to download the latest version of browsers. 

<script type="text/javascript">
var browser= x.appName
var version = parseFloat(x.appVersion)
if((browser="Netscape" || browser=="Microsoft Internet Explorer") && (version>=4))
{document.write("Your browser is good enough!")
 document.write("<br/>")}
else
{document.write("Your browser is old Update browser!")
 document.write("<br/>")}
</script>

Preview:

Read Next: What are the Different Ways to Redirect Page in JavaScript?

Author

Shuseel Baral is a web programmer and the founder of InfoTechSite has over 8 years of experience in software development, internet, SEO, blogging and marketing digital products and services is passionate about exceeding your expectations.

Comments are closed.