如何使用 jQuery 1.3 并弃用 $.browser.msie 进行浏览器检测?

发布于 2024-07-13 04:35:53 字数 402 浏览 12 评论 0原文

既然 jQuery 1.3 已弃用(我假设在未来版本中删除) $.browser.msie 和类似内容,应该如何进行浏览器检测?

我经常使用它来确定我们在哪个浏览器中对几乎所有浏览器进行 CSS 修复,例如:

$.browser.opera
$.browser.safari
$.browser.mozilla

... 好吧,我认为这就是所有浏览器 :)

我使用它的地方,我不确定是什么浏览器问题导致了这个问题,因为很多时候我只是试图修复浏览器中的 1 px 差异。

编辑:使用新的 jQuery 功能,无法确定您使用的是 IE6 还是 IE7。 现在应该如何确定这一点呢?

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?

I have used this a lot for determining which browser we are in for CSS fixes for pretty much every browser, such as:

$.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.

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(16

黎歌 2024-07-20 04:35:54

功能检测听起来不错,但是当我编写一些在特定浏览器中不起作用的代码时,最快的方法是浏览器检测。

使用自动边距水平居中元素适用于除 IE 之外的所有浏览器。 我要测试什么功能? 我不知道。 但我确实知道它不能在哪种浏览器中工作。

我可以理解为什么特征检测在某些情况下会很有用,例如。 如果浏览器处于怪异/严格模式。 最好的折衷方案是同时使用这两种方法。

鉴于此“我们建议不要使用此属性,请尝试使用功能检测”应该从 jQuery.browser 文档页面中删除。 这是误导性的。

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.

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.

不爱素颜 2024-07-20 04:35:54

为什么不能重写 jquery.browser 以在内部使用 jquery.support 来根据功能确定浏览器(可能在需要时补充 jquery.browser 中的当前方法),以提供选择浏览器系列和版本的便捷方法,同时也许比今天的 jquery.browser 更可靠?

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?

余生共白头 2024-07-20 04:35:54

jQuery 1.3 已取代浏览器测试

坦率地说,我很惊讶 Web 开发人员如此频繁地关心他们的网站运行在什么浏览器中。在 10 多年的 Web 开发中,我可以想到一些我关心的案例,更不用说费心做任何不同的事情了。 最常见的原因是 Firefox 和 IE 之间的命名字体大小差异很大(IE 中的 font-size:large 比 FF 中的大很多),因此我使用了 IEfix.css 文件来纠正那。

也许你应该看看 哪个更好:CSS hacks 或浏览器检测? 有关该主题的更深入的讨论。

简而言之,您应该关心某个功能是否受支持,而不是它是什么浏览器。

如果不知道你为什么关心它是否是 IE,就很难再说什么了,因为你可能会发现有一个更好的解决方案来完成你正在做的事情。

jQuery 1.3 has replaced browser testing.

Frankly I'm surprised how often Web developers are concerned about what browser their site is running in. In 10+ years of Web development I can think of a handful of cases where I've cared let alone bothered to do anything different. The most common reason has been that the named font sizes differ significantly between Firefox and IE (font-size: large is a lot larger in IE than FF) so I have used an IEfix.css file to correct that.

Perhaps you should look at What is better: CSS hacks or browser detection? for a more thorough discussion on the topic.

The long and short of it is you should care if a feature is supported or not, not which browser it is.

It's hard to say anything more without knowing why you care if it's IE because you'll probably find there's a much better solution to doing what you're doing.

乱世争霸 2024-07-20 04:35:54

尝试 http://code.labor8.eu/geckoFix 上的 GeckoFix 脚本,它会检测到 Firefox 较低版本比 3.0 更强大,因此您可以根据需要对其进行自定义(即添加更多规则,例如检测 Firefox 2、Firefox 3、Opera 和 Safari)。 我想这可能就是你正在寻找的。 要检查用户代理,只需在地址栏中输入 javascript:alert(navigator.userAgent) 并找到一些需要在脚本中输入的特定字符。

Try the GeckoFix script at http://code.labor8.eu/geckoFix , it detects the Firefox lower than 3.0 so you can customize it how you want (i.e. by adding more rules to it like detecting Firefox 2, Firefox 3, Opera and Safari). I think it could be what you're looking for. To check user agent just type in your address bar javascript:alert(navigator.userAgent) and find some specific characters you'll need to type in script.

回忆躺在深渊里 2024-07-20 04:35:54

我有一段 Javascript 可以在 Mozilla 和 Webkit 浏览器以及 IE8 中正常运行。 然而,在6和7中它就破裂了。 这与 CSS 无关,也与糟糕的 Javascript 无关,而是 IE<8 的糟糕支持。

我可以看到人们从哪里来检查“功能”而不是浏览器嗅探,但是,嗅探可用的功能与破坏代码无关,那么浏览器嗅探和功能嗅探之间有什么区别,直到$.support 对象中提供所有功能吗?

I've got a piece of Javascript that runs fine in Mozilla and Webkit browsers, as well as IE8. However, in 6 and 7 it breaks. This has nothing to do with the CSS, nor poor Javascript, but the crappy supports of IE<8.

I can see where people are coming from about checking for "features" as opposed to browser sniffing, however, the features that sniffing is available for are not relevant to the breaking code, so then what is the difference between browser sniffing and feature sniffing until ALL features are available in the $.support object?

如若梦似彩虹 2024-07-20 04:35:54

我想指出的是,navigator.userAgent 不太值得信赖,因为它很容易修改,并且可能不代表查看页面的实际浏览器。
这可能是 $.browser 最初被弃用的原因之一。

但为了解决这个问题,我们假设浏览器检测是绝对需要的

我遇到了 James Padolsey 的这个非常酷的片段,它实际上通过使用条件注释来区分 Internet Explorer。

我用上面的代码片段和 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));

这通过功能来区分浏览器。

唯一的问题是,我目前无法区分 Safari 和 Chrome(都是 Webkit 浏览器),以及 Gecko、Webkit 和 Opera 浏览器本身的版本。

我知道它并不完美,但它比 navigator.userAgent 略有改进。

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.

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.

I've compiled a small piece of code with above snippet and some code from 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.

I know it's not perfect, but it's a slight improvement over navigator.userAgent.

你是暖光i 2024-07-20 04:35:54

这可能不是 IE 6 最简洁的方法,但它确实有效并且易于理解:

$(document).ready(function() {
    ie6catch = $.browser.msie + $.browser.version;
    if (ie6catch.indexOf("true6") == -1) {
        alert("This is not Internet Explorer 6");
    }
});

This may not be the cleanest way for IE 6 but it certainly works and is easy to understand:

$(document).ready(function() {
    ie6catch = $.browser.msie + $.browser.version;
    if (ie6catch.indexOf("true6") == -1) {
        alert("This is not Internet Explorer 6");
    }
});
音盲 2024-07-20 04:35:54

一个好主意是将浏览器作为一个类添加到 css 的 body 标记中。 不确定这是否有效,因为我不运行 Windows 并且我有一台 powerPC,但它应该将 .ie6 类添加到所有 Internet Explorer 6-9,这不是很有帮助。 使用 mooModernizr 进行其他操作。

if (Browser.Engine.trident) {

    var IEbrowser = $('body');

    IEbrowser.addClass('ie');

}​

What would be a good idea is to add browsers as a class to the body tag for css. Not sure if this works because I don't run windows and I have a powerPC but it should put a class of .ie6 to all internet explorers 6-9, not very helpful. Use mooModernizr for everything else.

if (Browser.Engine.trident) {

    var IEbrowser = $('body');

    IEbrowser.addClass('ie');

}​
网名女生简单气质 2024-07-20 04:35:54
var browser = {
        chrome: false,
        mozilla: false,
        opera: false,
        msie: false,
        safari: false
    };
    var sBrowser, 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);
var browser = {
        chrome: false,
        mozilla: false,
        opera: false,
        msie: false,
        safari: false
    };
    var sBrowser, 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);
表情可笑 2024-07-20 04:35:53

是的,浏览器检测已被弃用,但已弃用的属性可能不会很快从 jQuery 中删除。 当它们被删除时,如果您仍然需要进行浏览器检测,您可以使用一个小而简单的插件轻松添加相同的功能。

所以,我的答案是暂时不做任何事情:)

编辑:我什至会为您提供一个将来使用的插件(未经测试,从 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);

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.

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):

(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);
为你鎻心 2024-07-20 04:35:53

我遇到了类似的问题,没有 $.support.png (p.ej.),所以我需要使用 $.browser.version,也许我们可以继续要求更多 $.support.XXXX 属性,尽可能多如所须。

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.

蛮可爱 2024-07-20 04:35:53

.browser 已被弃用,取而代之的是 .support。
更多信息在这里: jquery.support 这本质上意味着不使用浏览器嗅探,jquery 现在确实支持检测功能,并允许对浏览器可以执行的操作进行更细粒度的控制。

从描述来看:

在 jQuery 1.3 中添加了一个集合
代表存在的属性
不同的浏览器功能或错误。

jQuery 附带了许多
包括属性,你应该感觉到
自由添加您自己的。 其中许多
属性相当低级,所以
怀疑它们是否有用
在一般的日常开发中,但是
主要由插件和核心使用
开发人员。

所有支持的价值
属性是用确定的
特征检测(并且不使用任何
浏览器嗅探的形式)

.browser has been deprecated in favour of .support.
More information over here: jquery.support What 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.

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)

木格 2024-07-20 04:35:53

功能支持听起来是个好主意,但只有当它支持所有可能的“错误”时,它才会按预期工作。 就像第一个评论者一样,没有 $support.png,或 $support.stepping,或 $support.peekaboo,或,哦,这样的例子不胜枚举。
这样做的问题是,一些使某个浏览器兼容的代码将不可避免地最终由不需要它的浏览器执行。

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.

不喜欢何必死缠烂打 2024-07-20 04:35:53

jQuery 中并未弃用浏览器检测。 jQuery.browser 的文档页面,其中指出:

我们建议不要使用此属性,请尝试改用功能检测(请参阅 jQuery.support)。

弃用意味着“预计将来删除”。 这个关于嗅探功能而不是用户代理的建议只是很好的一般建议,而不是特定于 jQuery 的。 他们所说的只是让人们更容易做正确的事。

然而,总是需要用户代理嗅探。 除非jQuery.support由大量开发人员每天更新,否则它不可能跟上每个浏览器每个小版本中的每个错误和每个功能。

我认为对此的困惑源于这样一个事实:在内部,jQuery 不再进行浏览器嗅探。 但实用程序 API jQuery.browser 将继续存在。

Browser detection isn't deprecated in jQuery. Doc page for jQuery.browser, which states:

We recommend against using this property, please try to use feature detection instead (see 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.

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.

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.

葬花如无物 2024-07-20 04:35:53
function browserLessThanIE7(){
   return (/MSIE ((5\\.5)|6)/.test(navigator.userAgent) && navigator.platform == "Win32");
}

也可以正常工作...这个检查版本 5、5.5 和 6。

@Nate:将 (5\.5)|6 更改为 7,它会检查版本 7。

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.

@Nate: change (5\.5)|6 into 7 and it checks for version 7.

血之狂魔 2024-07-20 04:35:53

我说的是从 jQuery 1.2 的代码库对其进行逆向工程。

请参阅这部分代码:

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 )
};

尊重 GPL 和 MIT 许可证并从代码中学习。 不要复制和粘贴。

或者专门用于嗅出 IE6。 你可以这样做:

function IsThisBrowserIE6() {
    return ((window.XMLHttpRequest == undefined) && (ActiveXObject != undefined))
}

I say reverse engineer it from jQuery 1.2's codebase.

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.

Or specifically for smelling out IE6. You could do:

function IsThisBrowserIE6() {
    return ((window.XMLHttpRequest == undefined) && (ActiveXObject != undefined))
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文