Site icon Narayana Tutorial

How to detect browser details?


How to detect browser details?

Each and every browsers(IE, Mozilla, Chrome, Safari etc….) having own properties and own setup. Then while creating website we need to check out cross browser. Some of the functionality in the website we have to write logic according the browser specific. Then how to detect browser details? Let’s us understand it in this post.

The JavaScript that will detect the browser makes use of the navigator object. This object holds these interesting variables:

navigator Variables

VARIABLES DESCRIPTION
navigator.appCodeName The code name of the browser (e.g. Mozilla)
navigator.appName The name of the browser (e.g. Netscape or Microsoft Internet Explorer)
navigator.appVersion The browser version (e.g. 3.0 or 4.0)
navigator.userAgent The header information for the browser. (e.g. Mozilla/4.0)
navigator.platform The users operating system (e.g. WIN32)

Browser Detection Script

var nVer = navigator.appVersion;
var nAgt = navigator.userAgent;
var browserName  = navigator.appName;
var fullVersion  = ''+parseFloat(navigator.appVersion);
var majorVersion = parseInt(navigator.appVersion,10);
var nameOffset,verOffset,ix;

// In Opera, the true version is after “Opera” or after “Version”
if ((verOffset=nAgt.indexOf(“Opera”))!=-1) {
browserName = “O

How To Test

Example

<html>
<head>
<script type="text/javascript">
function browserDetails(){
alert("appName"+":"+navigator.appName+"\n"+
"appVersion"+":"+navigator.appVersion+"\n"+
"userAgent"+":"+navigator.userAgent);
}
</script>
</head>
<body onLoad=browserDetails()>
</body>
</html>

Copy and paste it in the file and save it as .html extension and open in the browser and see the output.

 

If you are satisfied or want to give any suggestion, please comment us in the below. 🙂