如何使用 javascript 检测 Internet Explorer 和较低版本?我知道 navigator 对象,但它发送的用于识别他的信息是什么?
最佳答案
我建议使用条件注释来加载针对 IE x.x 的 CSS 或 JS,如 @Jason McCreary 所建议的那样。但是,如果您深入研究 JS 插件或其他东西的代码并且需要它用于 JS,那么这是完成它的一种非常可靠的方式:
function getInternetExplorerVersion()
// Returns the version of Internet Explorer or a -1
// (indicating the use of another browser).
{
var rv = -1; // Return value assumes failure.
if (navigator.appName == 'Microsoft Internet Explorer')
{
var ua = navigator.userAgent;
var re = new RegExp("MSIE ([0-9]{1,}[\.0-9]{0,})");
if (re.exec(ua) != null)
rv = parseFloat( RegExp.$1 );
}
return rv;
}
如果您是极简主义者,那么有点叛逆;我相信这种方法确实有效:
if (typeof document.body.style.maxHeight != "undefined") {
// IE 7, moz, saf, opera 9
} else{
// IE6, older browsers
}
关于javascript - 如何用javascript检测IE6?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5940845/