如何使用 JavaScript 确定 Opera 浏览器

发布于 2024-08-16 08:08:21 字数 46 浏览 2 评论 0原文

我想使用JavaScript判断客户端机器的浏览器是否是Opera,该怎么做?

I want to determine whether the browser of the client machines is Opera using JavaScript, how to do that?

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

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

发布评论

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

评论(6

白云悠悠 2024-08-23 08:08:22

navigator 对象包含您需要的所有信息。这应该做:

navigator.userAgent.indexOf("Opera");

The navigator object contains all the info you need. This should do:

navigator.userAgent.indexOf("Opera");
掩耳倾听 2024-08-23 08:08:22

你介意使用 jQuery 吗?

那么你可以使用 jQuery.browser参见文档

但 jQuery 专家建议不要使用它。

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

编辑:

对于Mootools:使用 window.opera (请参阅文档

do you mind using jQuery?

then you can use jQuery.browser (see documnentation)

But the jQuery-guys recommend not to use this.

We recommend against using this
property, please try to use feature
detection instead (see jQuery.support)

Edit:

For Mootools: use window.opera (see documentation)

红焚 2024-08-23 08:08:21

现在 Opera 使用 Chrome 渲染引擎,公认的解决方案不再有效。

用户代理字符串显示如下:

Mozilla/5.0 (Macintosh; Intel Mac OS X 10_8_4) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/28.0.1500.52 Safari/537.36 OPR/15.0.1147.132

Opera 的唯一标识符是 OPR 部分。

这是我使用的代码,它应该与旧 Opera 或新 Opera 匹配。它使 Opera var 成为布尔值(true 或 false):

var Opera = (navigator.userAgent.match(/Opera|OPR\//) ? true : false);

Now that Opera uses the Chrome rendering engine, the accepted solution no longer works.

The User Agent string shows up like this:

Mozilla/5.0 (Macintosh; Intel Mac OS X 10_8_4) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/28.0.1500.52 Safari/537.36 OPR/15.0.1147.132

The only identifier for Opera is the OPR part.

Here's the code I use, which should match the old Opera or the new Opera. It makes the Opera var a boolean value (true or false):

var Opera = (navigator.userAgent.match(/Opera|OPR\//) ? true : false);

白馒头 2024-08-23 08:08:21
if(window.opera){
    //do stuffs, for example
    alert(opera.version()); //10.10 
}

不开玩笑,opera浏览器中有一个对象opera

您可能会认为,对象 opera 是可重写的,但 navigator 也是可重写的。

更新

为了获得更准确的结果,你可以这样做

if (window.opera && opera.toString() == "[object Opera]"){
    //do stuffs, tested on opera 10.10
}

我注意到,Opera都有addEventListener和attachEvent,所以还有另一种方法,例如

if (window.addEventListener && window.attachEvent){
    //do stuffs, tested on opera 10.10
}
if(window.opera){
    //do stuffs, for example
    alert(opera.version()); //10.10 
}

No kidding, there is an object opera in opera browser.

You may think, object opera is overridable, but navigator is overridable too.

UPDATE:

To get more accurate result, you could do like

if (window.opera && opera.toString() == "[object Opera]"){
    //do stuffs, tested on opera 10.10
}

And I noticed, Opera have both addEventListener and attachEvent, so there is also another way like

if (window.addEventListener && window.attachEvent){
    //do stuffs, tested on opera 10.10
}
烏雲後面有陽光 2024-08-23 08:08:21

上述答案在新的 Opera 30 中不再有效。因为 Opera 现在使用 Chromium。请使用以下内容:

var isChromium = window.chrome,
    isOpera = window.navigator.userAgent.indexOf("OPR") > -1 || window.navigator.userAgent.indexOf("Opera") > -1;
if(isChromium !== null && isOpera == true) {
   // is Opera (chromium)
} else { 
   // not Opera (chromium) 
}

新的 Opera 30 版本现已完全使用 Chromium,并将其 userAgent 更改为 OPR

The above answers no longer work in the new Opera 30. Since Opera now use Chromium. Please use the below:

var isChromium = window.chrome,
    isOpera = window.navigator.userAgent.indexOf("OPR") > -1 || window.navigator.userAgent.indexOf("Opera") > -1;
if(isChromium !== null && isOpera == true) {
   // is Opera (chromium)
} else { 
   // not Opera (chromium) 
}

The new Opera 30 release now fully uses Chromium and also changed their userAgent to OPR

拔了角的鹿 2024-08-23 08:08:21

在 Prototype.js 中,我们使用这个推论

var isOpera = Object.prototype.toString.call(window.opera) == '[object Opera]';

:本质上是检查 window.opera 对象是否存在并且其内部 [[Class]] 值为“Opera”。这是比仅仅检查 window.opera 是否存在更可靠的测试,因为一些不相关的全局 opera 变量妨碍并导致误报的可能性要小得多。

说到不相关的全局变量,请记住,例如在 MSHTML DOM 中,元素可以通过 id/name 进行全局解析;这意味着标记中出现类似 foo 的内容将导致 window.opera 引用该锚元素。这就是你的误报...

换句话说,测试 [[Class]] 值,而不仅仅是存在。

当然,在嗅探浏览器之前一定要三思而后行。通常有更好的方法来解决问题;)

PS Opera 的未来版本有可能更改 window.opera 的 [[Class]],但这似乎不太可能。

In Prototype.js, we use this inference:

var isOpera = Object.prototype.toString.call(window.opera) == '[object Opera]';

This essentially checks that window.opera object exists and its internal [[Class]] value is "Opera". This is a more solid test than just checking for window.opera existence, since there's much less chance of some unrelated global opera variable getting in the way and resulting in false positives.

Speaking of unrelated global variable, remember that in MSHTML DOM, for example, elements can be resolved by id/name globally; this means that presence of something like <a name="opera" href="...">foo</a> in a markup will result in window.opera referencing that anchor element. There's your false positive...

In other words, test [[Class]] value, not just existence.

And of course always think twice before sniffing for browser. Oftentimes there are better ways to solve a problem ;)

P.S. There's a chance of future versions of Opera changing [[Class]] of window.opera, but that seems to be unlikely.

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