jQuery $.browser 的替代品是什么
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/9645803/
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
What's the replacement for $.browser
提问by Rocky
The jQuery document tags $.browser
as deprecated. So what's the replacement for it?
$.browser
不推荐使用的 jQuery 文档标签。那么它的替代品是什么?
采纳答案by kapa
If you really need good old $.browser
如果你真的需要很好的旧 $.browser
According to the docs, this feature was deprecated in 1.3, and totally removed in 1.9, although it is still available in the official jQuery Migrate plugin.
根据文档,此功能在1.3中已弃用,并在1.9 中完全删除,尽管它仍可在官方 jQuery Migrate 插件中使用。
If you want to do it right
如果你想做得对
Depending on browser detectionis not a good idea. Feature detection is the way to go (Modernizris a great tool for that). jQuery had a $.support()
method to provide some feature detection, but it is now deprecated as well. They also suggest using Modernizer.
根据浏览器检测是不是一个好主意。特征检测是要走的路(Modernizr是一个很好的工具)。jQuery 有一种$.support()
方法可以提供一些特征检测,但现在也已弃用。他们还建议使用 Modernizer。
If you really need browser detection
如果你真的需要浏览器检测
Fixing browser quirks is not a valid use case for browser detection, but there are other use cases. Use any Javascript browser detection tool (like bowser), as this functionality does not depend on jQuery at all.
修复浏览器怪癖不是浏览器检测的有效用例,但还有其他用例。使用任何 Javascript 浏览器检测工具(如bowser),因为此功能根本不依赖于 jQuery。
回答by takien
Based on jQuery migration plugin, I found this.
基于jQuery 迁移插件,我发现了这个。
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"
};
};
if ( !jQuery.browser ) {
var
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 nnnnnn
There isn't a direct replacement. You should be using feature detection rather than browser detection (do you have a good reason to need to know the browser?), so you can use the $.support
property. (It says as much in the API doco for $.browser
.)
没有直接的替代品。您应该使用功能检测而不是浏览器检测(您有充分的理由需要了解浏览器吗?),因此您可以使用$.support
属性. (它在 API doco for 中说了同样多的内容$.browser
。)
回答by Emil Vikstr?m
回答by Spidfire
you can use navigator variable from javascript
您可以使用 javascript 中的 navigator 变量
console.log(navigator)
but if you want to check the compatiblility with a function of jquery you can use the support var like
但是如果你想检查与 jquery 函数的兼容性,你可以使用 support var like
$.support.ajax
回答by Claus
The jquery-browser-pluginis a good drop in replacement
在jQuery的浏览器插件是更换一个不错滴