js get browser name and platform

Solutions on MaxInterview for js get browser name and platform by the best coders in the world

showing results for - "js get browser name and platform"
Elona
03 Apr 2019
1var nVer = navigator.appVersion;
2var nAgt = navigator.userAgent;
3var browserName  = navigator.appName;
4var fullVersion  = ''+parseFloat(navigator.appVersion); 
5var majorVersion = parseInt(navigator.appVersion,10);
6var nameOffset,verOffset,ix;
7
8// In Opera, the true version is after "Opera" or after "Version"
9if ((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
16else 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" 
21else 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" 
26else 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" 
33else 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 
38else 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
48if ((ix=fullVersion.indexOf(";"))!=-1)
49   fullVersion=fullVersion.substring(0,ix);
50if ((ix=fullVersion.indexOf(" "))!=-1)
51   fullVersion=fullVersion.substring(0,ix);
52
53majorVersion = parseInt(''+fullVersion,10);
54if (isNaN(majorVersion)) {
55 fullVersion  = ''+parseFloat(navigator.appVersion); 
56 majorVersion = parseInt(navigator.appVersion,10);
57}
58
59document.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)
66
Maximilian
27 Jun 2017
1// This script sets OSName variable as follows:
2// "Windows"    for all versions of Windows
3// "MacOS"      for all versions of Macintosh OS
4// "Linux"      for all versions of Linux
5// "UNIX"       for all other UNIX flavors 
6// "Unknown OS" indicates failure to detect the OS
7
8var OSName="Unknown OS";
9if (navigator.appVersion.indexOf("Win")!=-1) OSName="Windows";
10if (navigator.appVersion.indexOf("Mac")!=-1) OSName="MacOS";
11if (navigator.appVersion.indexOf("X11")!=-1) OSName="UNIX";
12if (navigator.appVersion.indexOf("Linux")!=-1) OSName="Linux";
13
14document.write('Your OS: '+OSName);
15