jQuery 使用 jQuery.support 而不是 jQuery.browser 检测 IE6

发布于 2024-08-02 12:18:27 字数 186 浏览 5 评论 0原文

我没有找到使用 jquery.support 而不是不再支持的 jquery.browser 来检测 IE6 的明确方法。我还需要考虑怪癖模式,呃!!!

我在 Stack 上看到了几篇文章,但它们都引用了 jquery.browser 并且 jquery.support 的文档有点模糊。

对此的任何帮助都会很棒,并提前致谢。

I have found no definite way to detect IE6 using jquery.support instead of jquery.browser which is not supported anymore. Also I need to account for Quirks mode, Ugh!!!

I have seen a couple of posts on Stack but they all refer to jquery.browser and the documentation for jquery.support is a little vague.

Any help on this would be great and thanks in advance.

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

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

发布评论

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

评论(6

会发光的星星闪亮亮i 2024-08-09 12:18:27

我认为 jQuery simple 的作者不希望开发人员再检查哪个浏览器正在运行应用程序/网站。相反,他们希望我们检查不同的浏览器功能,并据此决定要做什么。无论是 IE6、IE8、Firefox 还是某些新浏览器,都没有关系。作为应用程序开发人员,您应该知道浏览器对不同功能的支持如何影响用户使用您的应用程序的能力,并且您应该采取相应的行动。

$.browser 依赖于使用 用户代理字符串 进行的浏览器嗅探,这可能不可靠,特别是因为用户很容易在浏览器上欺骗用户代理,而不是为了布局目的而担心它 - 如果他们更改了用户代理,那么他们的 IE6 将自己呈现为 IE7 无法正确呈现页面,这是他们的错。 ;)

您仍然可以使用 $.browser,尽管它已被弃用,或者尝试自己编写一些代码。也许已经有一个 jQuery 插件了?我知道上次我必须检查 IE6 我就使用了这个方法......

I think that the authors of jQuery simple don't want developers to check for which browser is running the application/website anymore. Instead they want us to check different browser features and decide on what to do based on that. It doesn't matter if it's IE6, IE8, Firefox or some new browser. As the application developer you should know how the browser support for different features affects the user's ability to work with your application and you should act accordingly.

$.browser depends on browser sniffing using the User Agent strings which may be unreliable, especially since it's very easy for users to spoof the User Agent on their browser, not that I would worry about it for layout purposes - if they changed their User Agent then it's their fault that their IE6 presenting itself as IE7 fails to render the page correctly. ;)

You can either still use $.browser, despite the fact that it's been deprecated or try to code something on your own. Maybe there's a jQuery plugin for that already? I know that last time I had to check for IE6 I used that method...

撕心裂肺的伤痛 2024-08-09 12:18:27

我没有找到使用 jquery.support 检测 IE6 的明确方法

不,当然不是 - 这就是重点。

教条是,检测对特定功能的支持,并针对缺乏这些功能的情况进行编码,是维护跨浏览器兼容性的更好方法,而不是试图跟踪每个存在的浏览器及其支持的内容。

虽然您可以从 jquery.support 向后猜测正在运行的浏览器,然后使用依赖于完全不同功能的不同代码,但这将是完全反常的。您将采用已经很脆弱的方法,假设浏览器“X”支持功能“Y”,并通过添加另一层不相关的功能嗅探(可能会发现错误的浏览器)使其变得更加脆弱。

所以教条的答案是,永远不要检测 IE6 或任何其他特定的浏览器。使用功能嗅探解决方案(例如 jquery.support)直接查看您可以使用哪些功能,但不要假设功能“Y”的存在或不存在意味着您可以或不能使用关联的不同功能“Z”使用相同的浏览器“X”。因为对于您以前从未遇到过的新的或晦涩的浏览器/版本,这总是会出错。

事实上,教条式的答案通常是正确的。但实际上,有些浏览器错误无法通过功能嗅探来检测,需要特定的解决方法。当然,这几乎总是一种浏览器,那就是 IE6。

目前尚不清楚您是否处于其中一种情况,因为您的问题中没有详细信息,但是当您确实需要对 That One Browser 进行可怕的黑客攻击时,大约最好的解决方案仍然是 条件注释,放入 JavaScript 或 HTML 中。

I have found no definite way to detect IE6 using jquery.support

No, of course not — that's the whole point.

The dogma is that detecting support for particular features, and coding around the lack of them, is a better way of maintaining cross-browser compatibility than trying to keep track of every browser that exists and what it supports.

Whilst you could work backwards from jquery.support to guess what browser is running, and then use different code that relies on a completely different feature, that would be utterly perverse. You'd be taking the already-fragile approach of assuming browser ‘X’ supports feature ‘Y’, and making it even more fragile by adding another layer of unrelated feature sniffing that could pick up the wrong browser.

So the dogma answer is, don't ever detect IE6, or any other particular browser. Use a feature-sniffing solution such as jquery.support to see directly what features you can use, but don't assume that the presence or absence of feature ‘Y’ means you can or can't use a different feature ‘Z’ associated with the same browser ‘X’. Because that will always go wrong given a new or obscure browser/version you haven't met before.

And the dogma answer is, indeed, usually right. However in reality there are some browser bugs that can't be detected by feature-sniffing and need specific workarounds. And of course the one browser this almost always is, is IE6.

It is unclear whether you are in one of those situations as there is no detail in your question, but when you really do need a horrible hack for That One Browser, approximately the best solution is still conditional comments, dropped into JavaScript or HTML.

不即不离 2024-08-09 12:18:27

Brad Koehler 有这个想法。使用像下面这样的 IE 条件语句

<!-- paulirish.com/2008/conditional-stylesheets-vs-css-hacks-answer-neither/ --> 
<!--[if lt IE 7 ]> <html lang="en" class="no-js ie6"> <![endif]-->
<!--[if IE 7 ]>    <html lang="en" class="no-js ie7"> <![endif]-->
<!--[if IE 8 ]>    <html lang="en" class="no-js ie8"> <![endif]-->
<!--[if IE 9 ]>    <html lang="en" class="no-js ie9"> <![endif]-->
<!--[if (gt IE 9)|!(IE)]><!--><html lang="en" class="no-js"><!--<![endif]-->

然后使用 javascript 和一点 jQuery 你就可以做到这一点..

$(document).ready(function () { 
    if ($('html').is('.ie6')) {
       alert('You are running IE6!');
    } else if($('html').is('.ie7')) {
    alert("You are running IE7!");
    } else {
       // Browsers other than IE6 and 7.
    }
});

编辑器正在蚕食我的第一部分代码,所以这里是它的链接 - http://paulirish.com/2008/conditional-stylesheets-vs-css-hacks-answer-neither/< /a>

Brad Koehler has the idea. Use IE conditional statements like those below

<!-- paulirish.com/2008/conditional-stylesheets-vs-css-hacks-answer-neither/ --> 
<!--[if lt IE 7 ]> <html lang="en" class="no-js ie6"> <![endif]-->
<!--[if IE 7 ]>    <html lang="en" class="no-js ie7"> <![endif]-->
<!--[if IE 8 ]>    <html lang="en" class="no-js ie8"> <![endif]-->
<!--[if IE 9 ]>    <html lang="en" class="no-js ie9"> <![endif]-->
<!--[if (gt IE 9)|!(IE)]><!--><html lang="en" class="no-js"><!--<![endif]-->

Then with javascript and a bit of jQuery you can then do this..

$(document).ready(function () { 
    if ($('html').is('.ie6')) {
       alert('You are running IE6!');
    } else if($('html').is('.ie7')) {
    alert("You are running IE7!");
    } else {
       // Browsers other than IE6 and 7.
    }
});

The editor is cannibalising my first section of code so here's the link to it - http://paulirish.com/2008/conditional-stylesheets-vs-css-hacks-answer-neither/

你げ笑在眉眼 2024-08-09 12:18:27

我刚才在一些测试中做了一些肮脏的事情,不想使用已弃用的 .browser 但没有阅读和理解 .support。

我最喜欢的是,使用条件样式表来为 IE6 设计样式。我只是简单地添加了一个 id="IE6",然后用 jQuery 检测到该 ID。

jQuery.fn.exists = function(){return jQuery(this).length>0;}
          if ($('#IE6').exists()) {
           //do something
          }

这似乎工作得很好,我尝试在那里发出警报,但只在 IE6 中得到它,我尝试验证页面,它作为 HTML5 传递,我不确定我所做的是否存在更深层次的潜在问题,但它到目前为止似乎还不错。

I have done something a bit dirty in some testing just now, not wanting to use the deprecated .browser but not having read and understood .support.

I like most, use a conditional stylesheet to style for IE6. I have simply added an id="IE6" to that and then detected the ID with jQuery.

jQuery.fn.exists = function(){return jQuery(this).length>0;}
          if ($('#IE6').exists()) {
           //do something
          }

This seems to work just fine, I tried firing an alert in there and only got it in IE6, I tried validating the page and it passes as HTML5, I am not sure if there is a deeper underlying problem with what I have done but it seems ok so far.

仄言 2024-08-09 12:18:27

您可以执行以下操作:

    var ie6to8 = jQuery.support.leadingWhitespace;
    var ie7to8 = jQuery.support.objectAll;
    if((ie6to8 && !ie7to8) || (!ie6to8 && ie7to8)) { //simulating a XOR
       // is ie6
    }

文档告诉 jQuery.support.leadingWhitespace 在 IE 6、7 和 8 上为 false。还说 objectAll 在 IE 7 和 8 上为 false。我假设其他浏览器也是如此,并进行了对这些值进行异或运算。这样,只有当leadingWhitespace 值与objectAll 不同时,它才会为真,这种情况只发生在IE6 中。

您可以在此处查看更多信息: http://docs.jquery.com/Utilities/jQuery.support< /a>

更新:从 jQuery 1.3 开始,不再推荐使用浏览器嗅探,但仍然可以用旧方法完成(使用 $.browser.msie 和 $.browser.version)。在这里查找:http://docs.jquery.com/Release:jQuery_1.3

You can do something like:

    var ie6to8 = jQuery.support.leadingWhitespace;
    var ie7to8 = jQuery.support.objectAll;
    if((ie6to8 && !ie7to8) || (!ie6to8 && ie7to8)) { //simulating a XOR
       // is ie6
    }

The documentation tells that jQuery.support.leadingWhitespace is false on IEs 6, 7 and 8. It's also said that objectAll is false on IE's 7 and 8. I assumed that it was true for the other browsers and made a XOR operation on those values. This way it will only be true when leadingWhitespace value is different from objectAll's, which only happens at IE6.

You can see more here: http://docs.jquery.com/Utilities/jQuery.support

UPDATE: As of jQuery 1.3 Browser Sniffing is no long recommended but still can be done the old way (using $.browser.msie and $.browser.version). Look it up here: http://docs.jquery.com/Release:jQuery_1.3

荭秂 2024-08-09 12:18:27

我知道这不符合您的“要求”,但据我所知,没有单独使用特征检测的可靠方法。

这是我发现的唯一可靠的检测 IE6 的方法。这是我在 SimpleModal 中使用的,看起来效果很好:

var ie6 = $.browser.msie && parseInt($.browser.version) === 6 && typeof window['XMLHttpRequest'] !== "object";

I know this doesn't fit your "requirements", but as far as I know, there isn't a reliable way using feature detection alone.

This is the only reliable that I have found to detect IE6. It's what I use in SimpleModal and it seems to work well:

var ie6 = $.browser.msie && parseInt($.browser.version) === 6 && typeof window['XMLHttpRequest'] !== "object";
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文