首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >更新到jQuery 1.9.1后出现browser.msie错误

更新到jQuery 1.9.1后出现browser.msie错误
EN

Stack Overflow用户
提问于 2013-02-15 17:55:50
回答 10查看 130.5K关注 0票数 55

我使用以下脚本片段:

if ($.browser.msie && $.browser.version < 9) {
   extra = "?" + Math.floor(Math.random() * 3000);
}

它在jQuery 1.8.3上运行良好。

现在,我将jQuery更新到新版本的1.9.1,以使用新脚本。

现在我得到以下错误:

TypeError:无法读取未定义的属性“”msie“”

我阅读了新jQuery版本的更改日志,但应该没有任何更改

和msie在一起

有任何已知的bug、提示或建议吗?

EN

回答 10

Stack Overflow用户

回答已采纳

发布于 2013-02-15 17:57:15

$.browser在1.3版中已弃用,并在1.9版中被删除

您可以通过查看documentation验证这一点。

票数 37
EN

Stack Overflow用户

发布于 2013-02-15 18:00:10

由于$.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;

}());
票数 58
EN

Stack Overflow用户

发布于 2013-12-29 22:15:49

对于简单的IE检测,我倾向于使用:

(/msie|trident/i).test(navigator.userAgent)

访问微软开发人员网络以了解IE用户代理:http://msdn.microsoft.com/library/ms537503.aspx

票数 14
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/14892095

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档