jquery - 更新到 jQuery 1.9.1 后 browser.msie 错误

标签 jquery version

我使用以下脚本片段:

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 );
}

Source

但是,它被弃用的原因是 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/

相关文章:

mysql - 检查 Mac 10.8.5 上的 mySQL 版本

android - Android 应用程序是否向后兼容?

javascript - 覆盖 javascript/jquery 中的 CMD+N 或 CTRL+N 快捷键?

德语文本中的 jQuery

javascript - jQuery拖放颜色div来改变另一个背景

.net - 为什么.NET中的System.Version定义为Major.Minor.Build.Revision?

javascript - 如何让 for 循环之后的代码在 for 循环完成其交互之前不运行?

javascript - 复制div,更改并输出到 View

python-2.7 - 从没有此类信息构建的 pyd 中检索 Python 版本

c++ - 使用以 10.0.0 形式定义版本号的宏