使用jQuery检测Opera的正确方法是什么? [英] What is the correct way to detect Opera using jQuery?

查看:116
本文介绍了使用jQuery检测Opera的正确方法是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Amazon.com最近更新了他们的javascript,这导致一些Opera浏览器出现问题。

Amazon.com recently updated their javascript, and it's causing problems with some Opera browsers.

他们的浏览器检测代码看起来像这样,但它有问题:

Their browser detection code looks like so, but it's faulty:

    function sitbReaderIsCompatibleBrowser() {
        if (typeof(jQuery) == 'undefined') {
            return false;
        } else {
            var version = jQuery.browser.version || "0";
            var splitVersion = version.split('.');
            return (
                   (jQuery.browser.msie && splitVersion[0] >= 6)  // IE 6 and higher
                || (jQuery.browser.mozilla && (
                       (splitVersion[0] == 1 && splitVersion[1] >= 8) // Firefox 2 and higher
                    || (splitVersion[0] >= 2)
                   ))
                || (jQuery.browser.safari && splitVersion[0] >= 500) // Safari 5 and higher
                || (jQuery.browser.opera && splitVersion[0] >= 9) // Opera 5 and higher
            );
        }
}

没有显然错误跳转用这段代码给我看,但我以前从未使用过jQuery,所以我不知道。

Nothing obviously wrong jumps out at me with this code, but I've never used jQuery before so I don't know.

即使这段代码看起来像是试图让Opera用户通过当我使用Opera 9.64访问该页面时,我收到不支持的浏览器消息。如果我更改Opera的设置以将自己报告为Firefox,则该页面可以完美运行!考虑到这一点,我很确定这是脚本而不是浏览器的问题。

Even though this code looks like it's attempting to let Opera users through, when I visit the page with Opera 9.64 I get an "unsupported browser" message. If I change Opera's settings to report itself as Firefox, the page works perfectly! With that in mind, I'm pretty sure it's a problem with the script and not the browser.

任何jQuery专家都有建议吗?

Any jQuery experts have a suggestion?

您可以通过访问亚马逊上的任何书籍并点击查看此书链接来复制该行为。

You can replicate the behavior by visiting any book on Amazon and clicking the "look inside this book" link.

推荐答案

在jQuery 1.3之前,您可以使用 jQuery.browser

Prior to jQuery 1.3, you could use jQuery.browser:

if( $.browser.opera ){
  alert( "You're using Opera version "+$.browser.version+"!" );
}

从版本1.3开始,你应该使用 jQuery.support

From version 1.3, you should use jQuery.support instead.

主要原因应该是避免检查浏览器,因为功能可能会随版本而变化,使您的代码很快就会过时。

Main reason for this is that should should avoid checking for browsers, as features may change from version to version, making your code obsolete in no time.

您应该始终尝试使用功能检测。这将允许您查看当前浏览器是否支持您尝试使用的功能,无论浏览器品牌,版本等等。

You should always try to use feature detection instead. This will allow you to see if current browser supports the feature you're trying to use, regardless the browser brand, version, etc.

这篇关于使用jQuery检测Opera的正确方法是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆