Home > Article > Web Front-end > How can I extract Firefox version information in Internet Explorer 6 using JavaScript?
Question:
The code provided doesn't retrieve the Firefox version number in Internet Explorer 6. How can I modify it to extract this information?
Answer:
The original code relies on browser-specific properties, such as navigator.appCodeName and navigator.appVersion. To ensure compatibility across different browsers, consider the following script:
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; // Check for specific browsers if ((verOffset = nAgt.indexOf("OPR")) != -1) { 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) { browserName = "Microsoft Edge"; fullVersion = nAgt.substring(verOffset + 4); } else if ((verOffset = nAgt.indexOf("MSIE")) != -1) { browserName = "Microsoft Internet Explorer"; fullVersion = nAgt.substring(verOffset + 5); } else if ((verOffset = nAgt.indexOf("Chrome")) != -1) { browserName = "Chrome"; fullVersion = nAgt.substring(verOffset + 7); } 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); } else if ((verOffset = nAgt.indexOf("Firefox")) != -1) { 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 version string 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); }
This script uses heuristics to extract browser name and version information across different browsers, including Internet Explorer 6.
The above is the detailed content of How can I extract Firefox version information in Internet Explorer 6 using JavaScript?. For more information, please follow other related articles on the PHP Chinese website!