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
Table of Contents
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?