如何在不推荐使用 $.browser.msie 的情况下使用 jQuery 1.3 进行浏览器检测?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/454875/
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
How to do browser detection with jQuery 1.3 with $.browser.msie deprecated?
提问by Darryl Hein
How should browser detection be done now that jQuery 1.3 has deprecated (and I'm assuming removed in a future version) $.browser.msie
and similar?
既然 jQuery 1.3 已弃用(我假设在未来版本中已删除)$.browser.msie
和类似版本,应该如何进行浏览器检测?
I have used this a lot for determining which browser we are in for CSS fixes for pretty much every browser, such as:
我经常使用它来确定我们使用哪个浏览器来修复几乎所有浏览器的 CSS,例如:
$.browser.opera
$.browser.safari
$.browser.mozilla
... well I think that's all of them :)
......好吧,我认为这就是全部:)
The places where I use it, I'm not sure what browser issue is causing the problem, because a lot of times I'm just trying to fix a 1 px
difference in a browser.
我使用它的地方,我不确定是什么浏览器问题导致了这个问题,因为很多时候我只是想修复1 px
浏览器中的差异。
Edit:With the new jQuery functionality, there is no way to determine if you are in IE6 or IE7. How should one determine this now?
编辑:使用新的 jQuery 功能,无法确定您使用的是 IE6 还是 IE7。现在应该如何确定这一点?
采纳答案by Ricardo Vega
I was facing something similar, there's no $.support.png (p.ej.), so I need to use the $.browser.version yet, maybe we can just keep asking for more $.support.XXXX properties, as much as needed.
我遇到了类似的问题,没有 $.support.png (p.ej.),所以我还需要使用 $.browser.version,也许我们可以继续要求更多 $.support.XXXX 属性,尽可能多如所须。
回答by kkyy
Yes, the browser detection has been deprecated, but the deprecated properties probably won't be removed from jQuery anytime soon. And when they will be removed, if you still absolutely need to do browser detection, you can add the same functionality easily with a small, simple plugin.
是的,浏览器检测已被弃用,但弃用的属性可能不会很快从 jQuery 中删除。当它们被删除时,如果您仍然绝对需要进行浏览器检测,您可以使用一个简单的小插件轻松添加相同的功能。
So, my answer is to do nothing about it, for now :)
所以,我的答案是暂时不做任何事情:)
edit: I'll even provide you with a plugin to use in the future (not tested, copy-pasted from jquery source):
编辑:我什至会为您提供一个插件以供将来使用(未测试,从 jquery 源复制粘贴):
(function($) {
var userAgent = navigator.userAgent.toLowerCase();
$.browser = {
version: (userAgent.match( /.+(?:rv|it|ra|ie)[\/: ]([\d.]+)/ ) || [0,'0'])[1],
safari: /webkit/.test( userAgent ),
opera: /opera/.test( userAgent ),
msie: /msie/.test( userAgent ) && !/opera/.test( userAgent ),
mozilla: /mozilla/.test( userAgent ) && !/(compatible|webkit)/.test( userAgent )
};
})(jQuery);
回答by Ray Booysen
.browser has been deprecated in favour of .support. More information over here: jquery.supportWhat this essentially means is that instead of using browser sniffing, jquery now does feature support detection and allows for much finer grained control over what the browser can do.
.browser 已被弃用,取而代之的是 .support。更多信息在这里:jquery.support这本质上意味着,jquery 现在支持检测功能,而不是使用浏览器嗅探,并允许对浏览器可以做什么进行更细粒度的控制。
From the description:
从描述来看:
Added in jQuery 1.3 A collection of properties that represent the presence of different browser features or bugs.
jQuery comes with a number of properties included, you should feel free to add your own. Many of these properties are rather low-level so it's doubtful that they'll be useful in general day-to-day development, but mostly used by plugin and core developers.
The values of all the support properties are determined using feature detection (and do not use any form of browser sniffing)
在 jQuery 1.3 中添加了代表不同浏览器功能或错误存在的属性集合。
jQuery 附带了许多属性,您可以随意添加自己的属性。许多这些属性相当低级,因此它们在一般的日常开发中是否有用是值得怀疑的,但主要由插件和核心开发人员使用。
所有支持属性的值都是使用特征检测来确定的(并且不使用任何形式的浏览器嗅探)
回答by Ray Booysen
feature support sounds a good idea, BUT it will only work as is intended when it supports all possible "bugs". Like the first commenter, there is no $support.png, or a $support.stepping, or a $support.peekaboo, or a, oh, the list goes on. The problem with this is that some code to make one browser compliant will inevitable end up being executed by a browser that does not need it.
功能支持听起来是个好主意,但它只有在支持所有可能的“错误”时才能按预期工作。像第一个评论者一样,没有 $support.png,或 $support.stepping,或 $support.peekaboo,或者,哦,这个列表还在继续。这样做的问题是,某些使浏览器兼容的代码将不可避免地最终被不需要它的浏览器执行。
回答by jpsimons
Browser detection isn'tdeprecated in jQuery. Doc page for jQuery.browser, which states:
jQuery 中不推荐使用浏览器检测。jQuery.browser 的文档页面,其中指出:
We recommend against using this property, please try to use feature detection instead (see jQuery.support).
我们建议不要使用此属性,请尝试使用功能检测代替(请参阅 jQuery.support)。
Deprecation means "slated for future removal." This advice about sniffing for capabilities rather than user agents is just good general advice, and not specific to jQuery. All they're saying is they're making it easier to do the right thing.
弃用意味着“计划在未来删除”。这个关于嗅探功能而不是用户代理的建议只是很好的一般建议,并不特定于 jQuery。他们所说的只是他们让做正确的事情变得更容易。
There's always going to be a need for user agent sniffing however. Unless jQuery.support
is updated daily by an army of developers, there's just no way it can keep up with every bug and every feature in every minor point version of every browser.
然而,总是需要用户代理嗅探。除非jQuery.support
由一大群开发人员每天更新,否则它无法跟上每个浏览器的每个小版本中的每个错误和每个功能。
I think the confusion about this arose from the fact that, internally, jQuery no longer does browser sniffing. But the utility API jQuery.browser will continue to exist.
我认为关于这一点的困惑源于这样一个事实,即在内部,jQuery 不再进行浏览器嗅探。但实用 API jQuery.browser 将继续存在。
回答by jpsimons
function browserLessThanIE7(){
return (/MSIE ((5\.5)|6)/.test(navigator.userAgent) && navigator.platform == "Win32");
}
Could also work fine... This one checks for version 5, 5.5 and 6.
也可以正常工作......这个检查版本 5、5.5 和 6。
@Nate: change (5\.5)|6 into 7 and it checks for version 7.
@Nate:将 (5\.5)|6 更改为 7 并检查版本 7。
回答by BuddyJoe
I say reverse engineer it from jQuery 1.2's codebase.
我说从 jQuery 1.2 的代码库逆向工程它。
See this section of the code:
请参阅代码的这一部分:
jQuery.browser = {
version: (userAgent.match( /.+(?:rv|it|ra|ie)[\/: ]([\d.]+)/ ) || [])[1],
safari: /webkit/.test( userAgent ),
opera: /opera/.test( userAgent ),
msie: /msie/.test( userAgent ) && !/opera/.test( userAgent ),
mozilla: /mozilla/.test( userAgent ) && !/(compatible|webkit)/.test( userAgent )
};
Respect the GPL and MIT licenses and learn from the code. Don't copy and paste.
尊重 GPL 和 MIT 许可并从代码中学习。不要复制和粘贴。
Or specifically for smelling out IE6. You could do:
或者专门用于嗅探IE6。你可以这样做:
function IsThisBrowserIE6() {
return ((window.XMLHttpRequest == undefined) && (ActiveXObject != undefined))
}
回答by john
Feature detection sounds all fine and dandy, but when I am writing some code that doesn't work in a certain browser, the quickest method is browser detection.
功能检测听起来不错,但当我编写一些在特定浏览器中不起作用的代码时,最快的方法是浏览器检测。
Horizontally centering an element using auto margins works for all browsers except IE. What feature do I test for? I have no idea. But I do know which browser it doesn't work in.
使用自动边距水平居中元素适用于除 IE 之外的所有浏览器。我要测试什么功能?我不知道。但我知道它在哪个浏览器中不起作用。
I can see why feature detection would be useful in certain situations eg. if a browser is in quirks/strict mode. The best compromise would be to utilise both methods.
我可以理解为什么特征检测在某些情况下会很有用,例如。如果浏览器处于怪癖/严格模式。最好的折衷办法是同时使用这两种方法。
In light of this "We recommend against using this property, please try to use feature detection instead" should be removed from the jQuery.browser documentation page. It is misleading.
鉴于此“我们建议不要使用此属性,请尝试改用功能检测”应从 jQuery.browser 文档页面中删除。这是误导。
回答by Matt Vaughan
Why couldn't jquery.browser be re-written to use jquery.support internally to determine the browser based on features (perhaps supplemented with current methods from jquery.browser where needed), to provide a convenient means to select for browser family and version, while perhaps being more reliable than today's jquery.browser?
为什么不能重新编写 jquery.browser 以在内部使用 jquery.support 来根据功能确定浏览器(可能在需要时补充来自 jquery.browser 的当前方法),以提供一种方便的方式来选择浏览器系列和版本,虽然可能比今天的 jquery.browser 更可靠?
回答by GeReV
I'd like to point out that navigator.userAgent
isn't very trustworthy, in the sense that it's easily modifiable and might not represent the actual browser viewing the page.
This might be one of the reasons $.browser
was deprecated in the first place.
我想指出这navigator.userAgent
不是很值得信赖,因为它很容易修改并且可能不代表查看页面的实际浏览器。这可能是$.browser
首先被弃用的原因之一。
But for the sake of the question, let's assume browser detection is absolutely needed.
但是为了这个问题,让我们假设绝对需要浏览器检测。
I ran into this very cool snippet by James Padolsey, which actually differentiates between Internet Explorers by using conditional comments.
我遇到了 James Padolsey 的这个非常酷的片段,它实际上通过使用条件注释来区分 Internet Explorer。
I've compiled a small piece of code with above snippet and some code from yepnope.js:
我用上面的代码片段和来自yepnope.js 的一些代码编译了一小段代码:
(function(window, doc) {
window.detector = window.detector || (function() {
var undef,
docElement = doc.documentElement,
v = 3,
div = document.createElement('div'),
all = div.getElementsByTagName('i'),
isGecko = ( 'MozAppearance' in docElement.style ),
isGeckoLTE18 = isGecko && !! doc.createRange().compareNode,
isOpera = !! ( window.opera && toString.call( window.opera ) == '[object Opera]' ),
isWebkit = ( 'webkitAppearance' in docElement.style ),
isNewerWebkit = isWebkit && 'async' in doc.createElement('script');
while (
div.innerHTML = '<!--[if gt IE ' + (++v) + ']><i></i><![endif]-->',
all[0]
);
return {
isGecko: isGecko,
isGeckoLTE18: isGeckoLTE18,
isGeckoGT18: isGecko && ! isGeckoLTE18,
isOpera: isOpera,
isWebkit: isWebkit,
isNewerWebkit: isWebkit && 'async' in doc.createElement('script'),
isIE: ( v > 4 ),
ieVersion: ( v > 4 ? v : undef )
};
}());
}(window, document));
This differentiates between browsers by their capabilities.
这通过浏览器的功能来区分浏览器。
Only problems are, I currently have no way of telling between Safari and Chrome (both Webkit browsers), and between the versions of the Gecko, Webkit and Opera browsers themselves.
唯一的问题是,我目前无法区分 Safari 和 Chrome(两种 Webkit 浏览器),以及 Gecko、Webkit 和 Opera 浏览器本身的版本。
I know it's not perfect, but it's a slight improvement over navigator.userAgent
.
我知道它并不完美,但比navigator.userAgent
.