浏览器检测 jQuery 1.9 最简单/最轻的替代品?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/14365725/
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
Easiest/Lightest Replacement For Browser Detection jQuery 1.9?
提问by jchwebdev
I had several clients complain yesterday that some code stopped working. Apparently it comes down to plug-ins using the now deprecated jQuery.browser
which stopped working yesterday when jQuery 1.9 was released.
昨天我有几个客户抱怨一些代码停止工作。显然,它归结为使用现已弃用的插件,该插件jQuery.browser
昨天在 jQuery 1.9 发布时停止工作。
I (quickly) looked at the 1.9 change docs and it seemslike they want me to substitute some pretty heavy libraries just for that onefunction.
我(快)看了一下1.9的变化文档和它看起来像他们想让我来代替一些非常沉重的图书馆只是那一个功能。
Is there a recommended lightest weight plug-in or code snippet to restore that functionality?
是否有推荐的最轻量级插件或代码片段来恢复该功能?
For what these sites need, it's very basic; I only need the most basic detection of IE vs FF vs everyone else.
对于这些网站所需要的,这是非常基本的;我只需要最基本的检测 IE vs FF vs 其他人。
Suggestions?
建议?
采纳答案by Fede
I've use the following code answered by Alexx Roche, but i wanted to detect MSIE so:
我使用了 Alexx Roche 回答的以下代码,但我想检测 MSIE:
<script type="text/javascript">
$(document).ready(function() {
if (navigator.userAgent.match(/msie/i) ){
alert('I am an old fashioned Internet Explorer');
}
});
</script>
hope it helps!
希望能帮助到你!
回答by Brad
jQuery Migrate was created to allow for backwards compatibilitywhile you update your code.
jQuery Migrate 的创建是为了在您更新代码时实现向后兼容性。
https://github.com/jquery/jquery-migrate
https://github.com/jquery/jquery-migrate
As a bonus, it logs deprecated functions as you use them. I would give it a try while you resolve the problems. Also, you should be setting a specific version of jQuery for your sites. It's good to upgrade, but be sure to test those upgrades before putting them in production. If you are using a CDN, you can still specify a specific version in the file name.
作为奖励,它会在您使用时记录已弃用的函数。当你解决问题时,我会尝试一下。此外,您应该为您的网站设置特定版本的 jQuery。升级很好,但一定要在将它们投入生产之前测试这些升级。如果您使用的是 CDN,您仍然可以在文件名中指定特定版本。
Now, you don't need a jQuery pluginfor what you are asking. Check out the navigator
object.
现在,您不需要 jQuery 插件来满足您的要求。检查出的navigator
对象。
appCodeName: "Mozilla"
appName: "Netscape"
appVersion: "5.0 (Macintosh; Intel Mac OS X 10_6_8) AppleWebKit/537.17 (KHTML, like Gecko) Chrome/24.0.1312.52 Safari/537.17"
cookieEnabled: true
doNotTrack: null
geolocation: Geolocation
language: "en-US"
mimeTypes: MimeTypeArray
onLine: true
platform: "MacIntel"
plugins: PluginArray
product: "Gecko"
productSub: "20030107"
userAgent: "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_6_8) AppleWebKit/537.17 (KHTML, like Gecko) Chrome/24.0.1312.52 Safari/537.17"
vendor: "Google Inc."
vendorSub: ""
回答by user989952
var browser = {
chrome: false,
mozilla: false,
opera: false,
msie: false,
safari: false
};
var sUsrAg = navigator.userAgent;
if(sUsrAg.indexOf("Chrome") > -1) {
browser.chrome = true;
} else if (sUsrAg.indexOf("Safari") > -1) {
browser.safari = true;
} else if (sUsrAg.indexOf("Opera") > -1) {
browser.opera = true;
} else if (sUsrAg.indexOf("Firefox") > -1) {
browser.mozilla = true;
} else if (sUsrAg.indexOf("MSIE") > -1) {
browser.msie = true;
}
console.log(browser.msie);
回答by Synthy
Put this code in your site (like js file, or after code of jQuery...):
将此代码放在您的站点中(例如 js 文件,或在 jQuery 代码之后...):
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 Alexx Roche
I've used the following code when I hit the same issue:
当我遇到同样的问题时,我使用了以下代码:
<script type="text/javascript">
$(document).ready(function() {
//if (!$.browser.webkit && ! $.browser.mozilla) { //depricated
if (!navigator.userAgent.match(/mozilla/i) &&
! navigator.userAgent.match(/webkit/i) ){
alert('Let me tell you about Mozilla');
}
});
</script>
回答by Kevin B
You could just not update until you move away from depreciated methods.
在您放弃折旧方法之前,您不能进行更新。
You really shouldn't be including jquery from a CDN without specifying a version number anyway, it in a way defeats the purpose of using a CDN (no cacheing).
你真的不应该在没有指定版本号的情况下从 CDN 中包含 jquery,它在某种程度上违背了使用 CDN(无缓存)的目的。
Here's a link to the latest version of jQuery that supported $.browser:
这是支持 $.browser 的最新版 jQuery 的链接:
http://code.jquery.com/jquery-1.8.3.min.js
http://code.jquery.com/jquery-1.8.3.min.js
simply replace your jquery.js src with that link and your code will continue to run until you are ready to move forward and stop using depreciated features.
只需将您的 jquery.js src 替换为该链接,您的代码将继续运行,直到您准备好继续前进并停止使用已弃用的功能。
Note: Fancybox2 still uses $.browser, that's the most common one I've seen so far since the update.
注意:Fancybox2 仍然使用 $.browser,这是更新以来我见过的最常见的一个。
Update: Slickgrid is still using $.browser
, no update as of 02/11/2013
更新:Slickgrid 仍在使用$.browser
,截至 2013 年 2 月11 日没有更新
回答by Gabriel Cebrian
I abstracted the logic from jQuery.browser into a plugin jquery.browser. The plugin is released under the MIT license.
我将 jQuery.browser 中的逻辑抽象为一个插件jquery.browser。该插件是在 MIT 许可下发布的。
I've also added support for IE11, Opera Webkit and Android detection.
我还添加了对 IE11、Opera Webkit 和 Android 检测的支持。
回答by Ijas Ameenudeen
回答by Vincent Gloaguen
Exact version of IE can be detected by additional checking of existence of standard global objects added in specific IE versions.
可以通过额外检查特定 IE 版本中添加的标准全局对象的存在来检测 IE 的确切版本。
10 or older document.all
9 or older document.all && !window.atob
8 or older document.all && !document.addEventListener
7 or older document.all && !document.querySelector
6 or older document.all && !window.XMLHttpRequest
5.x document.all && !document.compatMode
if (document.all && !document.querySelector) {
alert('IE7 or lower');
}
These tests avoid using the userAgent which is used for spoofing
这些测试避免使用用于欺骗的 userAgent
回答by etb
if(!$.browser){
$.browser={chrome:false,mozilla:false,opera:false,msie:false,safari:false};
var ua=navigator.userAgent;
$.each($.browser,function(c,a){
$.browser[c]=((new RegExp(c,'i').test(ua)))?true:false;
if($.browser.mozilla && c =='mozilla'){$.browser.mozilla=((new RegExp('firefox','i').test(ua)))?true:false;};
if($.browser.chrome && c =='safari'){$.browser.safari=false;};
});
};