使用 JavaScript 检测浏览器版本和操作系统
问题:
您需要检测使用 JavaScript 的浏览器版本和操作系统,但提供的代码无法在 Internet Explorer 中运行6.
解决方案:
您提供的代码依赖于 navigator 对象的特定属性,这些属性在 Internet Explorer 6 等旧版浏览器中不受支持。要解决此问题,我们可以使用更全面的方法来考虑不同的浏览器实现。
以下 JavaScript 代码片段提供了对浏览器版本和操作的详细分析系统:
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 "OPR" or after "Version" if ((verOffset=nAgt.indexOf("OPR"))!=-1) { browserName = "Opera"; fullVersion = nAgt.substring(verOffset+4); if ((verOffset=nAgt.indexOf("Version"))!=-1) fullVersion = nAgt.substring(verOffset+8); } // In MS Edge, the true version is after "Edg" in userAgent else if ((verOffset=nAgt.indexOf("Edg"))!=-1) { browserName = "Microsoft Edge"; fullVersion = nAgt.substring(verOffset+4); } // 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); } console.log('' +'Browser name = '+browserName+'<br>' +'Full version = '+fullVersion+'<br>' +'Major version = '+majorVersion+'<br>' +'navigator.appName = '+navigator.appName+'<br>' +'navigator.userAgent = '+navigator.userAgent+'<br>' );
示例输出:
对于 Firefox/12.0,输出将为:
Browser name = Firefox Full version = 12.0 Major version = 12 navigator.appName = Netscape navigator.userAgent = Mozilla/5.0 (Windows NT 5.1; rv:12.0) Gecko/20100101 Firefox/12.0
此方法全面检测各种浏览器和版本的浏览器版本和操作系统。
以上是如何在 Internet Explorer 6 中使用 JavaScript 检测浏览器版本和操作系统?的详细内容。更多信息请关注PHP中文网其他相关文章!