How to find browser name/details in JavaScript

{0 Comments}

To get the browser detail you can use the some in built function to get the name, version, user agent and other value of the navigator. Among then the user agent property is more  reliable than appname property to get the following is the small syntax.

 Javascript |  copy code |? 
1
var BrowserVersion = navigator.appVersion;
2
var Useragebt = navigator.userAgent;
3
var browserName  = navigator.appName;
4
var fullVersion  = ''+parseFloat(navigator.appVersion);

to implement the same you can use the following snippets which  you can use

 Javascript |  copy code |? 
01
var nVer = navigator.appVersion;
02
var nAgt = navigator.userAgent;
03
var browserName  = navigator.appName;
04
var fullVersion  = ''+parseFloat(navigator.appVersion); 
05
var majorVersion = parseInt(navigator.appVersion,10);
06
var nameOffset,verOffset,ix;
07
 
08
// In Opera, the true version is after "Opera" or after "Version"
09
if ((verOffset=nAgt.indexOf("Opera"))!=-1) {
10
 browserName = "Opera";
11
 fullVersion = nAgt.substring(verOffset+6);
12
 if ((verOffset=nAgt.indexOf("Version"))!=-1) 
13
   fullVersion = nAgt.substring(verOffset+8);
14
}
15
// In MSIE, the true version is after "MSIE" in userAgent
16
else if ((verOffset=nAgt.indexOf("MSIE"))!=-1) {
17
 browserName = "Microsoft Internet Explorer";
18
 fullVersion = nAgt.substring(verOffset+5);
19
}
20
// In Chrome, the true version is after "Chrome" 
21
else if ((verOffset=nAgt.indexOf("Chrome"))!=-1) {
22
 browserName = "Chrome";
23
 fullVersion = nAgt.substring(verOffset+7);
24
}
25
// In Safari, the true version is after "Safari" or after "Version" 
26
else if ((verOffset=nAgt.indexOf("Safari"))!=-1) {
27
 browserName = "Safari";
28
 fullVersion = nAgt.substring(verOffset+7);
29
 if ((verOffset=nAgt.indexOf("Version"))!=-1) 
30
   fullVersion = nAgt.substring(verOffset+8);
31
}
32
// In Firefox, the true version is after "Firefox" 
33
else if ((verOffset=nAgt.indexOf("Firefox"))!=-1) {
34
 browserName = "Firefox";
35
 fullVersion = nAgt.substring(verOffset+8);
36
}
37
// In most other browsers, "name/version" is at the end of userAgent 
38
else if ( (nameOffset=nAgt.lastIndexOf(' ')+1) < 
39
          (verOffset=nAgt.lastIndexOf('/')) ) 
40
{
41
 browserName = nAgt.substring(nameOffset,verOffset);
42
 fullVersion = nAgt.substring(verOffset+1);
43
 if (browserName.toLowerCase()==browserName.toUpperCase()) {
44
  browserName = navigator.appName;
45
 }
46
}
47
// trim the fullVersion string at semicolon/space if present
48
if ((ix=fullVersion.indexOf(";"))!=-1)
49
   fullVersion=fullVersion.substring(0,ix);
50
if ((ix=fullVersion.indexOf(" "))!=-1)
51
   fullVersion=fullVersion.substring(0,ix);
52
 
53
majorVersion = parseInt(''+fullVersion,10);
54
if (isNaN(majorVersion)) {
55
 fullVersion  = ''+parseFloat(navigator.appVersion); 
56
 majorVersion = parseInt(navigator.appVersion,10);
57
}
58
 
59
document.write(''
60
 +'Browser name  = '+browserName+'<br>'
61
 +'Full version  = '+fullVersion+'<br>'
62
 +'Major version = '+majorVersion+'<br>'
63
 +'navigator.appName = '+navigator.appName+'<br>'
64
 +'navigator.userAgent = '+navigator.userAgent+'<br>'
65
)

Leave a Comment

Your email address will not be published.