我使用以下脚本片段:
if ($.browser.msie && $.browser.version < 9) {
extra = "?" + Math.floor(Math.random() * 3000);
}
它与 jQuery 1.8.3 配合良好。
现在我将 jQuery 更新到新版本 1.9.1 以使用新脚本。
现在我收到以下错误:
TypeError: Cannot read property 'msie' of undefined
我阅读了新 jQuery 版本的更改日志,但没有任何变化
与msie
有任何已知的错误、提示或建议吗?
最佳答案
由于 $.browser 已被弃用,这里有一个替代解决方案:
/**
* Returns the version of Internet Explorer or a -1
* (indicating the use of another browser).
*/
function getInternetExplorerVersion()
{
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;
}
function checkVersion()
{
var msg = "You're not using Internet Explorer.";
var ver = getInternetExplorerVersion();
if ( ver > -1 )
{
if ( ver >= 8.0 )
msg = "You're using a recent copy of Internet Explorer."
else
msg = "You should upgrade your copy of Internet Explorer.";
}
alert( msg );
}
但是,它被弃用的原因是 jQuery 希望您使用 feature detection反而。
一个例子:
$("p").html("This frame uses the W3C box model: <span>" +
jQuery.support.boxModel + "</span>");
最后但并非最不重要的一点是,检查 IE 版本的最可靠方法:
// ----------------------------------------------------------
// A short snippet for detecting versions of IE in JavaScript
// without resorting to user-agent sniffing
// ----------------------------------------------------------
// If you're not in IE (or IE version is less than 5) then:
// ie === undefined
// If you're in IE (>=5) then you can determine which version:
// ie === 7; // IE7
// Thus, to detect IE:
// if (ie) {}
// And to detect the version:
// ie === 6 // IE6
// ie > 7 // IE8, IE9 ...
// ie < 9 // Anything less than IE9
// ----------------------------------------------------------
// UPDATE: Now using Live NodeList idea from @jdalton
var ie = (function(){
var undef,
v = 3,
div = document.createElement('div'),
all = div.getElementsByTagName('i');
while (
div.innerHTML = '<!--[if gt IE ' + (++v) + ']><i></i><![endif]-->',
all[0]
);
return v > 4 ? v : undef;
}());
关于jquery - 更新到 jQuery 1.9.1 后 browser.msie 错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14892095/