Home > Article > Web Front-end > How to Detect Browser Version and Operating System with JavaScript Across All Major Browsers?
Your current implementation only works in Chrome and Mozilla because it utilizes properties that are not consistently supported across all browsers. To obtain a comprehensive solution that works on all major browsers, follow these steps:
Retrieve Browser Details:
var nVer = navigator.appVersion; var nAgt = navigator.userAgent; var browserName = navigator.appName;
Extract Version Number:
Use the indexOf() method to locate the starting position of the version within the user agent string.
var verOffset = nAgt.indexOf("Firefox"); // for Firefox
Extract the version number by slicing the user agent string starting from the identified position.
var fullVersion = nAgt.substring(verOffset + 8);
Parse and Trim Version String:
Remove any trailing characters beyond a semicolon (;) or space ( ) in the extracted string.
if ((ix = fullVersion.indexOf(";")) != -1) fullVersion = fullVersion.substring(0, ix); if ((ix = fullVersion.indexOf(" ")) != -1) fullVersion = fullVersion.substring(0, ix);
Extract Major Version:
Parse the version string into an integer.
var majorVersion = parseInt(fullVersion, 10);
Display Results:
document.write('' + 'Browser name = ' + browserName + '<br>' + 'Full version = ' + fullVersion + '<br>' + 'Major version = ' + majorVersion + '<br>' + 'navigator.appName = ' + navigator.appName + '<br>' + 'navigator.userAgent = ' + navigator.userAgent + '<br>' );
By using the provided snippet, you can accurately detect the browser version and operating system across multiple browsers and present the results in a user-friendly format.
The above is the detailed content of How to Detect Browser Version and Operating System with JavaScript Across All Major Browsers?. For more information, please follow other related articles on the PHP Chinese website!