js get browser name and platform 1

js get browser name and platform

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 = "Opera";
 fullVersion = nAgt.substring(verOffset+6);
 if ((verOffset=nAgt.indexOf("Version"))!=-1) 
   fullVersion = nAgt.substring(verOffset+8);
}
// In MSIE, the true version is after "MSIE" in userAgent
else if ((verOffset=nAgt.indexOf("MSIE"))!=-1) {
 browserName = "Microsoft Internet Explorer";
 fullVersion = nAgt.substring(verOffset+5);
}
// In Chrome, the true version is after "Chrome" 
else if ((verOffset=nAgt.indexOf("Chrome"))!=-1) {
 browserName = "Chrome";
 fullVersion = nAgt.substring(verOffset+7);
}
// In Safari, the true version is after "Safari" or after "Version" 
else if ((verOffset=nAgt.indexOf("Safari"))!=-1) {
 browserName = "Safari";
 fullVersion = nAgt.substring(verOffset+7);
 if ((verOffset=nAgt.indexOf("Version"))!=-1) 
   fullVersion = nAgt.substring(verOffset+8);
}
// In Firefox, the true version is after "Firefox" 
else if ((verOffset=nAgt.indexOf("Firefox"))!=-1) {
 browserName = "Firefox";
 fullVersion = nAgt.substring(verOffset+8);
}
// In most other browsers, "name/version" is at the end of userAgent 
else if ( (nameOffset=nAgt.lastIndexOf(' ')+1) < 
          (verOffset=nAgt.lastIndexOf('/')) ) 
{
 browserName = nAgt.substring(nameOffset,verOffset);
 fullVersion = nAgt.substring(verOffset+1);
 if (browserName.toLowerCase()==browserName.toUpperCase()) {
  browserName = navigator.appName;
 }
}
// trim the fullVersion string at semicolon/space if present
if ((ix=fullVersion.indexOf(";"))!=-1)
   fullVersion=fullVersion.substring(0,ix);
if ((ix=fullVersion.indexOf(" "))!=-1)
   fullVersion=fullVersion.substring(0,ix);

majorVersion = parseInt(''+fullVersion,10);
if (isNaN(majorVersion)) {
 fullVersion  = ''+parseFloat(navigator.appVersion); 
 majorVersion = parseInt(navigator.appVersion,10);
}

document.write(''
 +'Browser name  = '+browserName+'
' +'Full version = '+fullVersion+'
' +'Major version = '+majorVersion+'
' +'navigator.appName = '+navigator.appName+'
' +'navigator.userAgent = '+navigator.userAgent+'
' )

Here is what the above code is Doing:
1. It first checks to see if the browser is Opera. If it is, it sets the browser name to Opera and the full version to the substring starting at the 6th character in the userAgent string. It then checks to see if the browser is Opera and the version is after “Version”. If it is, it sets the full version to the substring starting at the 8th character in the userAgent string.
2. It then checks to see if the browser is IE. If it is, it sets the browser name to Microsoft Internet Explorer and the full version to the substring starting at the 5th character in the userAgent string.
3. It then checks to see if the browser is Chrome. If it is, it sets the browser name to Chrome and the full version to the substring starting at the 7th character in the userAgent string.
4. It then checks to see if the browser is Safari. If it is, it sets the browser name to Safari and the full version to the substring starting at the 7th character in the userAgent string. It then checks to see if the browser is Safari and the version is after “Version”. If it is, it sets the full version to the substring starting at the 8th character in the userAgent string.
5. It then checks to see if the browser is Firefox. If it is, it sets the browser name to Firefox and the full version to the substring starting at the 8th character in the userAgent string.
6. It then checks to see if the browser is any other browser. If it is, it sets the browser name to the substring starting at the last space in the userAgent string and ending at the last slash in the userAgent string. It then sets the full version to the substring starting at the last slash in the userAgent string and ending at the end of the userAgent string. If the browser name is all uppercase, it sets the browser name to the navigator.appName property.
7. It then trims the full version string at the semicolon or space if present.
8. It then sets the major version to the full version. If the major version is not a number, it sets the full version to the navigator.appVersion property and the major version to the navigator.appVersion property.
9. It then writes the browser name, full version, and major version to the page.

Similar Posts