jQuery 1.9 - 如何从 $.browser.msie 迁移?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/18277773/
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
jQuery 1.9 - How to migrate from $.browser.msie?
提问by Ian Vink
I am updating an old project that references $.browser.msie. Moving to jQuery 1.9of course breaks this.
我正在更新一个引用$.browser.msie的旧项目。迁移到jQuery 1.9当然会打破这一点。
How can I rewrite this code to get the same boolean value without having to include jQuery Migrate?
如何重写此代码以获得相同的布尔值而不必包含 jQuery Migrate?
The code is buried deep in an old javascript library we use that needs to determine if msie is running and then works on the knowledge. We'd rather not edit the javascript too much as it's brittle.
代码深埋在我们使用的旧 javascript 库中,需要确定 msie 是否正在运行,然后处理知识。我们宁愿不要过多地编辑 javascript,因为它很脆弱。
回答by Trevor Dixon
You could consider including the relevant code from jQuery 1.8 (https://github.com/jquery/jquery/blob/1.8.3/src/deprecated.js:
您可以考虑包含 jQuery 1.8 ( https://github.com/jquery/jquery/blob/1.8.3/src/deprecated.js ) 中的相关代码:
(function() {
var matched, browser;
// Use of jQuery.browser is frowned upon.
// More details: http://api.jquery.com/jQuery.browser
// jQuery.uaMatch maintained for back-compat
jQuery.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"
};
};
matched = jQuery.uaMatch( navigator.userAgent );
browser = {};
if ( matched.browser ) {
browser[ matched.browser ] = true;
browser.version = matched.version;
}
// Chrome is Webkit, but Webkit is also Safari.
if ( browser.chrome ) {
browser.webkit = true;
} else if ( browser.webkit ) {
browser.safari = true;
}
jQuery.browser = browser;
})();
回答by Samuel Liew
The jQuery.browser() method has been deprecated since jQuery 1.3 and is removed in 1.9. If needed, it is available as part of the jQuery Migrate plugin.
jQuery.browser() 方法自 jQuery 1.3 起已被弃用,并在 1.9 中删除。如果需要,它可以作为jQuery Migrate 插件的一部分使用。
See also jQuery Core 1.9 Upgrade Guide
回答by nirazul
If you really need to do this. There is a hacky, ugly and quite painless way: Put this into your HTML. It will set $.browser.msie for you.
如果你真的需要这样做。有一种笨拙、丑陋且非常轻松的方法:将其放入您的 HTML 中。它将为您设置 $.browser.msie。
Detect IE except IE10:
检测IE10以外的IE:
<!--[if IE]>
<script type="text/javascript">
$(function(){
if(!$.browser && !$.browser.msie) { $.browser = {}; }
else { return; }
$.browser.msie = true;
});
</script>
<![endif]-->
Detect all IE including IE10
检测包括IE10在内的所有IE
<!--[if IE]>
<script type="text/javascript">
$(function(){
$('body').addClass('msie');
});
</script>
<![endif]-->
<script type="text/javascript">
$(function(){
var $body = $('body');
// use this only if you need to detect IE10
if (Function('/*@cc_on return document.documentMode===10@*/')()){
$body.addClass('msie');
}
if($body.hasClass('msie')) {
if(!$.browser && !$.browser.msie) { $.browser = {}; }
else { return; }
$.browser.msie = true;
}
});
</script>