How to Detect Browser Version and Operating System Using JavaScript
Issue:
When using the provided code, browser detection works in Chrome and Mozilla but fails in IE6. The requirement is to specifically extract the browser version, such as "Firefox/12.0," from the user agent string.
Solution:
To effectively detect browser version and operating system using JavaScript, a more comprehensive approach is necessary. Here's a script that addresses the issue:
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; // Determine browser name and version based on user agent string if ((verOffset = nAgt.indexOf("OPR")) != -1) { // Opera browserName = "Opera"; fullVersion = nAgt.substring(verOffset + 4); if ((verOffset = nAgt.indexOf("Version")) != -1) { fullVersion = nAgt.substring(verOffset + 8); } } else if ((verOffset = nAgt.indexOf("Edg")) != -1) { // Microsoft Edge browserName = "Microsoft Edge"; fullVersion = nAgt.substring(verOffset + 4); } else if ((verOffset = nAgt.indexOf("MSIE")) != -1) { // Microsoft Internet Explorer browserName = "Microsoft Internet Explorer"; fullVersion = nAgt.substring(verOffset + 5); } else if ((verOffset = nAgt.indexOf("Chrome")) != -1) { // Chrome browserName = "Chrome"; fullVersion = nAgt.substring(verOffset + 7); } else if ((verOffset = nAgt.indexOf("Safari")) != -1) { // Safari browserName = "Safari"; fullVersion = nAgt.substring(verOffset + 7); if ((verOffset = nAgt.indexOf("Version")) != -1) { fullVersion = nAgt.substring(verOffset + 8); } } else if ((verOffset = nAgt.indexOf("Firefox")) != -1) { // Firefox browserName = "Firefox"; fullVersion = nAgt.substring(verOffset + 8); } 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 fullVersion string to remove semicolon or space if present if ((ix = fullVersion.indexOf(";")) != -1) { fullVersion = fullVersion.substring(0, ix); } if ((ix = fullVersion.indexOf(" ")) != -1) { fullVersion = fullVersion.substring(0, ix); } // Convert fullVersion to number and fall back to navigator.appVersion if conversion fails majorVersion = parseInt("" + fullVersion, 10); if (isNaN(majorVersion)) { fullVersion = "" + parseFloat(navigator.appVersion); majorVersion = parseInt(navigator.appVersion, 10); } // Display browser and version information document.write( "" + 'Browser name = ' + browserName + "<br>" + 'Full version = ' + fullVersion + "<br>" + 'Major version = ' + majorVersion + "<br>" + 'navigator.appName = ' + navigator.appName + "<br>" + 'navigator.userAgent = ' + navigator.userAgent + "<br>" );
This updated script provides a more robust method for detecting browser version and operating system by analyzing the user agent string thoroughly. It handles different browser types and versions, including Internet Explorer 6. By parsing the user agent string, this script accurately extracts the desired browser version, such as "Firefox/12.0," while also providing detailed information about the browser and system.
The above is the detailed content of How to Reliably Detect Browser Version and Operating System Using JavaScript?. For more information, please follow other related articles on the PHP Chinese website!