更新到 jQuery 1.9.1 后的 browser.msie 错误

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/14892095/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-26 14:02:52  来源:igfitidea点击:

browser.msie error after update to jQuery 1.9.1

jqueryversion

提问by Michael Schmidt

I use the following snip of a script:

我使用以下脚本片段:

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

It works fine with jQuery 1.8.3.

它适用于jQuery 1.8.3

Now I updated jQuery to the new version 1.9.1to use a new script.
Now I get the following error:

现在我将 jQuery 更新到新版本1.9.1以使用新脚本。
现在我收到以下错误:

TypeError: Cannot read property 'msie' of undefined

类型错误:无法读取未定义的属性“msie”

I read the change log of the new jQuery version, but nothing should have changed
with msie

我读的新的jQuery版本的更改记录,但没有早该换
MSIE

Any known bugs, tips or proposals?

任何已知的错误、提示或建议?

采纳答案by kicaj

$.browserwas deprecated in version 1.3 and removed in 1.9

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

You can verify this by viewing the documentation.

您可以通过查看文档来验证这一点。

回答by Johan

Since $.browser is deprecated, here is an alternative solution:

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

    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

来源

However, the reason that its deprecated is because jQuery wants you to use feature detectioninstead.

然而,它被弃用的原因是因为 jQuery 希望你改用特征检测

An example:

一个例子:

$("p").html("This frame uses the W3C box model: <span>" +
        jQuery.support.boxModel + "</span>");

And last but not least, the most reliable way to check IE versions:

最后但并非最不重要的是,检查 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;

}());

回答by Arun P Johny

The jQuery.browseroptions was deprecated earlier and removed in 1.9 release along with a lot of other deprecated items like .live.

jQuery.browser选项前面已经废弃,在1.9版本有很多其他弃用的项目像一起被移除.live

For projects and external libraries which want to upgrade to 1.9 but still want to support these features jQuery have release a migration plugin for the time being.

对于想要升级到 1.9 但仍希望支持这些功能的项目和外部库,jQuery 暂时发布了一个迁移插件。

If you need backward compatibility you can use migration plugin.

如果您需要向后兼容,您可以使用迁移插件

回答by user3144038

For simple IE detection I tend to use:

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

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

Visit the Microsoft Developer Network to learn about the IE useragent: http://msdn.microsoft.com/library/ms537503.aspx

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

回答by Kishore

Include jQuery migration pluginalong with your jQuery library.

包括jQuery 迁移插件以及您的 jQuery 库。

回答by SpYk3HH

Update! Complete answer overhaul for new plugin!

更新!新插件的完整答案大修!

The following plugin has been tested in all major browsers. It makes traditional use of userAgentstring to re-equip jQuery.browseronly if you're using jQuery version 1.9 or Greater!

以下插件已在所有主要浏览器中进行了测试。仅当您使用 jQuery 1.9 或更高版本时,它才使用传统的userAgent字符串来重新装备!jQuery.browser

It has the traditional jQuery.browser.msietype properties as well as a few new ones, including a .mobileproperty to help decide if user is on a mobile device.

它具有传统的jQuery.browser.msie类型属性以及一些新的.mobile属性,包括帮助确定用户是否在移动设备上的属性。

Note: This is not a suitable replacement for feature testing. If you expect to support a specific feature on a specific device, it's still best to use traditional feature testing

注意:这不是功能测试的合适替代品。如果您希望在特定设备上支持特定功能,最好仍然使用传统的功能测试

/** jQuery.browser
 * @author J.D. McKinstry (2014)
 * @description Made to replicate older jQuery.browser command in jQuery versions 1.9+
 * @see http://jsfiddle.net/SpYk3/wsqfbe4s/
 *
 * @extends jQuery
 * @namespace jQuery.browser
 * @example jQuery.browser.browser == 'browserNameInLowerCase'
 * @example jQuery.browser.version
 * @example jQuery.browser.mobile @returns BOOLEAN
 * @example jQuery.browser['browserNameInLowerCase']
 * @example jQuery.browser.chrome @returns BOOLEAN
 * @example jQuery.browser.safari @returns BOOLEAN
 * @example jQuery.browser.opera @returns BOOLEAN
 * @example jQuery.browser.msie @returns BOOLEAN
 * @example jQuery.browser.mozilla @returns BOOLEAN
 * @example jQuery.browser.webkit @returns BOOLEAN
 * @example jQuery.browser.ua @returns navigator.userAgent String
 */
;;(function($){var a=$.fn.jquery.split("."),b;for(b in a)a[b]=parseInt(a[b]);if(!$.browser&&(1<a[0]||9<=a[1])){a={browser:void 0,version:void 0,mobile:!1};navigator&&navigator.userAgent&&(a.ua=navigator.userAgent,a.webkit=/WebKit/i.test(a.ua),a.browserArray="MSIE Chrome Opera Kindle Silk BlackBerry PlayBook Android Safari Mozilla Nokia".split(" "),/Sony[^ ]*/i.test(a.ua)?a.mobile="Sony":/RIM Tablet/i.test(a.ua)?a.mobile="RIM Tablet":/BlackBerry/i.test(a.ua)?a.mobile="BlackBerry":/iPhone/i.test(a.ua)?
a.mobile="iPhone":/iPad/i.test(a.ua)?a.mobile="iPad":/iPod/i.test(a.ua)?a.mobile="iPod":/Opera Mini/i.test(a.ua)?a.mobile="Opera Mini":/IEMobile/i.test(a.ua)?a.mobile="IEMobile":/BB[0-9]{1,}; Touch/i.test(a.ua)?a.mobile="BlackBerry":/Nokia/i.test(a.ua)?a.mobile="Nokia":/Android/i.test(a.ua)&&(a.mobile="Android"),/MSIE|Trident/i.test(a.ua)?(a.browser="MSIE",a.version=/MSIE/i.test(navigator.userAgent)&&0<parseFloat(a.ua.split("MSIE")[1].match(/[0-9\.]{1,}/)[0])?parseFloat(a.ua.split("MSIE")[1].match(/[0-9\.]{1,}/)[0]):
"Edge",/Trident/i.test(a.ua)&&/rv:([0-9]{1,}[\.0-9]{0,})/.test(a.ua)&&(a.version=parseFloat(a.ua.match(/rv:([0-9]{1,}[\.0-9]{0,})/)[1].match(/[0-9\.]{1,}/)[0]))):/Chrome/.test(a.ua)?(a.browser="Chrome",a.version=parseFloat(a.ua.split("Chrome/")[1].split("Safari")[0].match(/[0-9\.]{1,}/)[0])):/Opera/.test(a.ua)?(a.browser="Opera",a.version=parseFloat(a.ua.split("Version/")[1].match(/[0-9\.]{1,}/)[0])):/Kindle|Silk|KFTT|KFOT|KFJWA|KFJWI|KFSOWI|KFTHWA|KFTHWI|KFAPWA|KFAPWI/i.test(a.ua)?(a.mobile="Kindle",
/Silk/i.test(a.ua)?(a.browser="Silk",a.version=parseFloat(a.ua.split("Silk/")[1].split("Safari")[0].match(/[0-9\.]{1,}/)[0])):/Kindle/i.test(a.ua)&&/Version/i.test(a.ua)&&(a.browser="Kindle",a.version=parseFloat(a.ua.split("Version/")[1].split("Safari")[0].match(/[0-9\.]{1,}/)[0]))):/BlackBerry/.test(a.ua)?(a.browser="BlackBerry",a.version=parseFloat(a.ua.split("/")[1].match(/[0-9\.]{1,}/)[0])):/PlayBook/.test(a.ua)?(a.browser="PlayBook",a.version=parseFloat(a.ua.split("Version/")[1].split("Safari")[0].match(/[0-9\.]{1,}/)[0])):
/BB[0-9]{1,}; Touch/.test(a.ua)?(a.browser="Blackberry",a.version=parseFloat(a.ua.split("Version/")[1].split("Safari")[0].match(/[0-9\.]{1,}/)[0])):/Android/.test(a.ua)?(a.browser="Android",a.version=parseFloat(a.ua.split("Version/")[1].split("Safari")[0].match(/[0-9\.]{1,}/)[0])):/Safari/.test(a.ua)?(a.browser="Safari",a.version=parseFloat(a.ua.split("Version/")[1].split("Safari")[0].match(/[0-9\.]{1,}/)[0])):/Firefox/.test(a.ua)?(a.browser="Mozilla",a.version=parseFloat(a.ua.split("Firefox/")[1].match(/[0-9\.]{1,}/)[0])):
/Nokia/.test(a.ua)&&(a.browser="Nokia",a.version=parseFloat(a.ua.split("Browser")[1].match(/[0-9\.]{1,}/)[0])));if(a.browser)for(var c in a.browserArray)a[a.browserArray[c].toLowerCase()]=a.browser==a.browserArray[c];$.extend(!0,$.browser={},a)}})(jQuery);
/* - - - - - - - - - - - - - - - - - - - */

var b = $.browser;
console.log($.browser);    //    see console, working example of jQuery Plugin
console.log($.browser.chrome);

for (var x in b) {
    if (x != 'init')
        $('<tr />').append(
            $('<th />', { text: x }),
            $('<td />', { text: b[x] })
        ).appendTo($('table'));
}
table { border-collapse: collapse; }
th, td { border: 1px solid; padding: .25em .5em; vertical-align: top; }
th { text-align: right; }

textarea { height: 500px; width: 100%; }
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
<table></table>

回答by user3137694

You can detect the IE browser by this way.

您可以通过这种方式检测IE浏览器。

(navigator.userAgent.toLowerCase().indexOf('msie 6') != -1)

(navigator.userAgent.toLowerCase().indexOf('msie 6') != -1)

you can get reference on this URL: jquery.browser.msie Alternative

您可以在此 URL 上获得参考:jquery.browser.msie Alternative

回答by Steven.Nguyen

Using this:

使用这个:

if (navigator.userAgent.match("MSIE")) {}

if (navigator.userAgent.match("MSIE")) {}

回答by Lucks

You can use :

您可以使用 :

var MSIE = jQuery.support.leadingWhitespace; // This property is not supported by ie 6-8

$(document).ready(function(){

if (MSIE){
    if (navigator.vendor == 'Apple Computer, Inc.'){
        // some code for this navigator
    } else {
       // some code for others browsers
    }

} else {
    // default code

}});

回答by Johan

Instead of having the whole migration script added, you could simply add the following (extracted from the migration script)

您可以简单地添加以下内容(从迁移脚本中提取),而不是添加整个迁移脚本

$.uaMatch = function( ua ) {
    ua = ua.toLowerCase();

    var match = /(chrome)[ \/]([\w.]+)/.exec( ua ) ||
        /(webkit)[ \/]([\w.]+)/.exec( ua ) ||
        /(opera)(?:.*version|)[ \/]([\w.]+)/.exec( ua ) ||
        /(msie) ([\w.]+)/.exec( ua ) ||
        ua.indexOf("compatible") < 0 && /(mozilla)(?:.*? rv:([\w.]+)|)/.exec( ua ) ||
        [];

    return {
        browser: match[ 1 ] || "",
        version: match[ 2 ] || "0"
    };
};

and then use it like so

然后像这样使用它

$.uaMatch(navigator.userAgent)