不推荐使用 jQuery $.browser 吗?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/9638247/
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
Is jQuery $.browser Deprecated?
提问by Mark_54
Can someone tell me if I am correct in believing that jQuery's $.browser is/has been deprecated?
有人能告诉我我认为 jQuery 的 $.browser 是否/已被弃用是正确的吗?
Will my existing implementations continue to work? If not, is there an easy to implement alternative.
我现有的实现会继续工作吗?如果没有,是否有易于实施的替代方案。
采纳答案by lonesomeday
From the documentation:
从文档:
The $.browser property is deprecated in jQuery 1.3, and its functionality may be moved to a team-supported plugin in a future release of jQuery.
$.browser 属性在 jQuery 1.3 中已弃用,其功能可能会在 jQuery 的未来版本中移至团队支持的插件中。
So, yes, it is deprecated, but your existing implementations will continue to work. If the functionality is removed, it will likely be easily accessible using a plugin.
所以,是的,它已被弃用,但您现有的实现将继续工作。如果删除了该功能,则很可能可以使用插件轻松访问它。
As to whether there is an alternative... The answer is "yes, probably". It is far, far better to do feature detection using $.support
rather than browser detection: detect the actual feature you need, not the browser that provides it. Most important features that vary from browser to browser are detected with that.
至于是否有替代方案……答案是“有,可能”。使用$.support
而不是浏览器检测进行特征检测要好得多:检测您需要的实际特征,而不是提供它的浏览器。与浏览器不同的大多数重要功能都可以通过它检测到。
Update 16 February 2013:In jQuery 1.9, this feature was removed (docs). It is far better not to use it. If you really, really mustuse its functionality, you can restore it with the jQuery Migrateplugin.
2013年2 月 16 日更新:在 jQuery 1.9 中,删除了此功能 ( docs)。最好不要使用它。如果您真的,真的必须使用它的功能,您可以使用jQuery Migrate插件恢复它。
回答by EminezArtus
Second Question
第二个问题
Will my existing implementations continue to work? If not, is there an easy to implement alternative.
我现有的实现会继续工作吗?如果没有,是否有易于实施的替代方案。
The answer is yes, but not without a little work.
答案是肯定的,但并非没有一点工作。
$.browser is an official plugin which was included in older versions of jQuery, so like any plugin you can simple copy it and incorporate it into your project or you can simply add it to the end of any jQuery release.
$.browser 是一个官方插件,它包含在旧版本的 jQuery 中,所以像任何插件一样,您可以简单地复制它并将其合并到您的项目中,或者您可以简单地将它添加到任何 jQuery 版本的末尾。
I have extracted the code for you incase you wish to use it.
我已经为您提取了代码,以防您希望使用它。
// Limit scope pollution from any deprecated API
(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;
jQuery.sub = function() {
function jQuerySub( selector, context ) {
return new jQuerySub.fn.init( selector, context );
}
jQuery.extend( true, jQuerySub, this );
jQuerySub.superclass = this;
jQuerySub.fn = jQuerySub.prototype = this();
jQuerySub.fn.constructor = jQuerySub;
jQuerySub.sub = this.sub;
jQuerySub.fn.init = function init( selector, context ) {
if ( context && context instanceof jQuery && !(context instanceof jQuerySub) ) {
context = jQuerySub( context );
}
return jQuery.fn.init.call( this, selector, context, rootjQuerySub );
};
jQuerySub.fn.init.prototype = jQuerySub.fn;
var rootjQuerySub = jQuerySub(document);
return jQuerySub;
};
})();
If you're asking why anyone would need a depreciated plugin, I have prepared the following answer.
如果您问为什么有人需要折旧的插件,我准备了以下答案。
First and foremost the answer is compatibility. Since jQuery is plugin based, some developers opted to use $.browser and with the latest releases of jQuery which doesn't include $.browser all those plugins where rendered useless.
首先,答案是兼容性。由于 jQuery 是基于插件的,一些开发人员选择使用 $.browser 和最新版本的 jQuery,其中不包含 $.browser 的所有那些插件都无用。
jQuery did release a migration plugin, which was created for developers to detect whether their plugin's used any depreciated dependencies such as $.browser.
jQuery 确实发布了一个迁移插件,它是为开发人员创建的,用于检测他们的插件是否使用了任何贬值的依赖项,例如 $.browser。
Although this helped developers patch their plugin's. jQuery dropped $.browser completely so the above fix is probably the only solution until your developers patch or incorporate the above.
尽管这有助于开发人员修补他们的插件。jQuery 完全删除了 $.browser,因此在您的开发人员修补或合并上述内容之前,上述修复可能是唯一的解决方案。
About:jQuery.browser
回答by Ismael Miguel
Here I present an alternative way to detect a browser, based on feature availability.
在这里,我提出了一种基于功能可用性检测浏览器的替代方法。
To detect only IE, you can use this:
要仅检测 IE,您可以使用以下命令:
if(/*@cc_on!@*/false || typeof ScriptEngineMajorVersion === "function")
{
//You are using IE>=4 (unreliable for IE11)
}
else
{
//You are using other browser
}
To detect the most popular browsers:
检测最流行的浏览器:
if(/*@cc_on!@*/false || typeof ScriptEngineMajorVersion === "function")
{
//You are using IE >= 4 (unreliable for IE11!!!)
}
else if(window.chrome)
{
//You are using Chrome or Chromium
}
else if(window.opera)
{
//You are using Opera >= 9.2
}
else if('MozBoxSizing' in document.body.style)
{
//You are using Firefox or Firefox based >= 3.2
}
else if({}.toString.call(window.HTMLElement).indexOf('Constructor')+1)
{
//You are using Safari >= 3.1
}
else
{
//Unknown
}
This answer was updated because IE11 no longer supports conditional compilation (the /*@cc_on!@*/false
trick).
You can check Did IE11 remove javascript conditional compilation?for more informations regarding this topic.
I've used the suggestion they presented there.
Alternatively, you can use typeof document.body.style.msTransform == "string"
or document.body.style.msTransform !== window.undefined
or even 'msTransform' in document.body.style
.
此答案已更新,因为 IE11 不再支持条件编译(/*@cc_on!@*/false
技巧)。
您可以检查IE11是否删除了javascript条件编译?有关此主题的更多信息。
我已经使用了他们在那里提出的建议。
另外,您也可以使用typeof document.body.style.msTransform == "string"
或者document.body.style.msTransform !== window.undefined
甚至'msTransform' in document.body.style
。
回答by SpYk3HH
Updated! 3/24/2015 (scroll below hr)
更新!2015 年 3 月 24 日(滚动至小时下方)
lonesomeday's answeris absolutely correct, I just thought I would add this tidbit. I had made a method a while back for getting browser in Vanilla JS and eventually curved it to replace jQuery.browser
in later versions of jQuery. It does not interfere with any part of the new jQuery lib, but provides the same functionality of the traditional jQuery.browser
object, as well as some other little features.
lonesomeday的答案是绝对正确的,我只是想我会添加这个花絮。不久前,我制定了一种方法,用于在 Vanilla JS 中获取浏览器,并最终将其修改为jQuery.browser
在更高版本的 jQuery 中替换。它不会干扰新 jQuery 库的任何部分,但提供与传统jQuery.browser
对象相同的功能,以及其他一些小功能。
New Extended Version!
新的扩展版本!
Is much more thorough for newer browser. Also, 90+% accuracy on mobile testing! I won't say 100%, as I haven't tested on every mobile browser, but new feature adds $.browser.mobile
boolean/string. It's false if not mobile, else it will be a String name for the mobile device or browser (Best Guesss like: Android, RIM Tablet, iPod, etc...).
对于较新的浏览器来说更加彻底。此外,移动测试的准确率高达 90% 以上!我不会说 100%,因为我还没有在每个移动浏览器上测试过,但新功能添加了$.browser.mobile
布尔值/字符串。如果不是移动设备,则为 false,否则它将是移动设备或浏览器的字符串名称(最佳猜测如:Android、RIM 平板电脑、iPod 等...)。
One possible caveat, may not work with some older (unsupported) browsers as it is completely reliant on userAgent
string.
一个可能的警告,可能不适用于某些较旧的(不受支持的)浏览器,因为它完全依赖于userAgent
字符串。
JS Minified
JS 缩小版
/* quick & easy cut & paste */
;;(function($){if(!$.browser&&1.9<=parseFloat($.fn.jquery)){var 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].replace(/[^0-9\.]/g,""))?parseFloat(a.ua.split("MSIE")[1].replace(/[^0-9\.]/g,"")):"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].replace(/[^0-9\.]/g,"")))):/Chrome/.test(a.ua)?(a.browser="Chrome",a.version=parseFloat(a.ua.split("Chrome/")[1].split("Safari")[0].replace(/[^0-9\.]/g,""))):/Opera/.test(a.ua)?(a.browser="Opera",a.version=parseFloat(a.ua.split("Version/")[1].replace(/[^0-9\.]/g,""))):/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].replace(/[^0-9\.]/g,""))):/Kindle/i.test(a.ua)&&/Version/i.test(a.ua)&&(a.browser="Kindle",a.version=parseFloat(a.ua.split("Version/")[1].split("Safari")[0].replace(/[^0-9\.]/g,"")))):/BlackBerry/.test(a.ua)?(a.browser="BlackBerry",a.version=parseFloat(a.ua.split("/")[1].replace(/[^0-9\.]/g,""))):/PlayBook/.test(a.ua)?(a.browser="PlayBook",a.version=parseFloat(a.ua.split("Version/")[1].split("Safari")[0].replace(/[^0-9\.]/g,""))):/BB[0-9]{1,}; Touch/.test(a.ua)?(a.browser="Blackberry",a.version=parseFloat(a.ua.split("Version/")[1].split("Safari")[0].replace(/[^0-9\.]/g,""))):/Android/.test(a.ua)?(a.browser="Android",a.version=parseFloat(a.ua.split("Version/")[1].split("Safari")[0].replace(/[^0-9\.]/g,""))):/Safari/.test(a.ua)?(a.browser="Safari",a.version=parseFloat(a.ua.split("Version/")[1].split("Safari")[0].replace(/[^0-9\.]/g,""))):/Firefox/.test(a.ua)?(a.browser="Mozilla",a.version=parseFloat(a.ua.split("Firefox/")[1].replace(/[^0-9\.]/g,""))):/Nokia/.test(a.ua)&&(a.browser="Nokia",a.version=parseFloat(a.ua.split("Browser")[1].replace(/[^0-9\.]/g,""))));if(a.browser)for(var b in a.browserArray)a[a.browserArray[b].toLowerCase()]=a.browser==a.browserArray[b];$.extend(!0,$.browser={},a)}})(jQuery);
/* quick & easy cut & paste */
jsFiddle "jQuery Plugin: Get Browser (Extended Alt Edition)"
jsFiddle“jQuery 插件:获取浏览器(扩展 Alt 版)”
/** 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($){if(!$.browser&&1.9<=parseFloat($.fn.jquery)){var 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].replace(/[^0-9\.]/g,""))?parseFloat(a.ua.split("MSIE")[1].replace(/[^0-9\.]/g,"")):"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].replace(/[^0-9\.]/g,"")))):/Chrome/.test(a.ua)?(a.browser="Chrome",a.version=parseFloat(a.ua.split("Chrome/")[1].split("Safari")[0].replace(/[^0-9\.]/g,""))):/Opera/.test(a.ua)?(a.browser="Opera",a.version=parseFloat(a.ua.split("Version/")[1].replace(/[^0-9\.]/g,""))):/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].replace(/[^0-9\.]/g,""))):/Kindle/i.test(a.ua)&&/Version/i.test(a.ua)&&(a.browser="Kindle",a.version=parseFloat(a.ua.split("Version/")[1].split("Safari")[0].replace(/[^0-9\.]/g,"")))):/BlackBerry/.test(a.ua)?(a.browser="BlackBerry",a.version=parseFloat(a.ua.split("/")[1].replace(/[^0-9\.]/g,""))):/PlayBook/.test(a.ua)?(a.browser="PlayBook",a.version=parseFloat(a.ua.split("Version/")[1].split("Safari")[0].replace(/[^0-9\.]/g,""))):/BB[0-9]{1,}; Touch/.test(a.ua)?(a.browser="Blackberry",a.version=parseFloat(a.ua.split("Version/")[1].split("Safari")[0].replace(/[^0-9\.]/g,""))):/Android/.test(a.ua)?(a.browser="Android",a.version=parseFloat(a.ua.split("Version/")[1].split("Safari")[0].replace(/[^0-9\.]/g,""))):/Safari/.test(a.ua)?(a.browser="Safari",a.version=parseFloat(a.ua.split("Version/")[1].split("Safari")[0].replace(/[^0-9\.]/g,""))):/Firefox/.test(a.ua)?(a.browser="Mozilla",a.version=parseFloat(a.ua.split("Firefox/")[1].replace(/[^0-9\.]/g,""))):/Nokia/.test(a.ua)&&(a.browser="Nokia",a.version=parseFloat(a.ua.split("Browser")[1].replace(/[^0-9\.]/g,""))));if(a.browser)for(var b in a.browserArray)a[a.browserArray[b].toLowerCase()]=a.browser==a.browserArray[b];$.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 Gabriel Santos
"The $.browser property is deprecated in jQuery 1.3, and its functionality may be moved to a team-supported plugin in a future release of jQuery."
“$.browser 属性在 jQuery 1.3 中已被弃用,其功能可能会在 jQuery 的未来版本中移至团队支持的插件中。”
回答by Jaime Montoya
From the official documentation at http://api.jquery.com/jQuery.browser/:
来自http://api.jquery.com/jQuery.browser/的官方文档:
This property was removed in jQuery 1.9 and is available only through the jQuery.migrate plugin.
此属性已在 jQuery 1.9 中删除,并且只能通过 jQuery.migrate 插件使用。
You can use for example jquery-migrate-1.4.1.js
to keep your existing code or plugins that use $.browser still working while you find a way to totally get rid of $.browser from your code in the future.
例如jquery-migrate-1.4.1.js
,您可以使用保持现有代码或使用 $.browser 的插件仍然有效,同时您可以找到一种方法在将来完全摆脱代码中的 $.browser。