The window.navigator object contains information about the visitor’s browser and the Navigator interface represents the state and the identity of the user agent. We can use a navigator to query it and to register themselves to carry on some activities.
Example of Javascript detect browser
Let create an HTML file to demonstrate how we can use detect browser type using Javascript code.
<!DOCTYPE html>
<html>
<head>
<script>
function detectBrowser() {
if (navigator.userAgent.toLowerCase().indexOf('chrome') > -1) {
alert("Chrome");
} else if (navigator.userAgent.toLowerCase().indexOf('edg') > -1) {
alert("Edge");
} else if (navigator.userAgent.toLowerCase().indexOf('firefox') > -1) {
alert('Firefox');
} else if (navigator.userAgent.toLowerCase().indexOf('safari') > -1) {
alert('Safari');
} else if ((navigator.userAgent.indexOf('opera') || navigator.userAgent.indexOf('OPR')) != -1) {
alert('Opera');
} else if (navigator.userAgent.indexOf("MSIE") != -1) {
alert('IE');
} else {
alert('none');
}
}
</script>
</head>
<body>
<h2>Javascript Detect mobile and desktop device</h2>
<button type="button" onclick="detectBrowser()">Detect Browser</button>
</body>
</html>
Javascript code to determine chrome version
function getChromeVersion () {
var raw = navigator.userAgent.match(/Chrom(e|ium)\/([0-9]+)\./);
return raw ? parseInt(raw[2], 10) : false;
}
alert(getChromeVersion ());
Related posts
- Javascript Map and Join function
- JavaScript String – replace() Method
- Functional Programming in JavaScript
- The JavaScript an Array function
How to use Javascript detect browser type