How to Get Browser Name in JavaScript

Get browser name in JavaScript

How to Get Browser Name in JavaScript

Browsers are essential for the web development and web developers as well. Most of the browsers have their own rendering engines. These web browsers rendering engines sometimes make a very big difference in displaying a web page. As a developer you have to work very hard to display a web page alike in all the web browsers. Sometimes you may want to set a specific CSS style for a browser. There is much functionality that differs in browsers. That is the reason why you should know that how to get browser name in JavaScript.

Get browser name in JavaScript

Now we are going to discuss that how to get browser name in JavaScript. With the help of JavaScript we can check that what, is the name of this browser. You can also use Jquery instead of JavaScript but we will show you that how to get browser name in JavaScript.

Now let us see a code example to understand it more.

How to get browser name in JavaScript example code:

browser detection code:

<!DOCTYPE html>

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

<head>

<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />

<title>Untitled Document</title>

<script type="text/javascript">

var Firefox = typeof InstallTrigger !== 'undefined';

var Safari = Object.prototype.toString.call(window.HTMLElement).indexOf('Constructor') > 0;

var Opera = !!window.opera || navigator.userAgent.indexOf(' OPR/') >= 0;

var Chrome = !!window.chrome && !Opera;

var IE = false || !!document.documentMode;

window.onload = function(){

if (Firefox){

alert("You are using Firefox.");

}else if (Safari){

alert("You are using Safari.");

}else if (Opera){

alert("You are using Opera.");

}else if (Chrome){

alert("You are using Chrome.");

}else if(IE){

alert("You are using Internet Explorer.");

}else{

alert("You are not using Firfox, Safari, Opera, Chrome or Internet Explorer.");

}

}

</script>

</head>

 

<body>

</body>

</html>

In the above code we are checking browsers with JavaScript. We are checking the major browsers that are famous on the internet these days. You can copy the code and can use it anywhere you want.

If you want to just check the IE (Internet explorer) then here is another way to check the IE version as well. Let us see the code example for how to check IE version with JavaScript and Jquery.

how to get browser version in JavaScript:

<!DOCTYPE html>

<!--[if IE 7 ]>   <html class="ie7"> <![endif]-->

<!--[if IE 8 ]>   <html class="ie8"> <![endif]-->

<!--[if IE 9 ]>   <html class="ie9"> <![endif]-->

<!--[if (gt IE 9)|!(IE)]> <html class=""> <![endif]-->

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

<head>

<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />

<title>Untitled Document</title>

<script src="//ajax.googleapis.com/ajax/libs/jquery/1.11.2/jquery.min.js"></script>

<script type="text/javascript">

window.onload = function() {

if ($('html').is('.ie6')) {

alert("You are using Internet Explorer version 6");

}else if($('html').is('.ie7')){

alert("You are using Internet Explorer version 7");

}else if($('html').is('.ie8')){

alert("You are using Internet Explorer version 8");

}else if($('html').is('.ie9')){

alert("You are using Internet Explorer version 9");

}else{

alert("You have Internet Explorer version greater than IE9 or you are not using IE");        }

};

</script>

</head>

 

<body>

</body>

</html>

In the code we are checking what is the version of IE we are using. This is another very helpful code for developers. Thanks and enjoy the codes.

See more coding problems solved here on howtocode.pk

Comments