如何使用 JavaScript 确定 Opera 浏览器

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1998293/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-22 22:18:54  来源:igfitidea点击:

How to determine the Opera browser using JavaScript

javascriptcross-browseropera

提问by Avinash

I want to determine that the browser of the client machines in Opera or not using JavaScript, how to do that?

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

采纳答案by dxh

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

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

navigator.userAgent.indexOf("Opera");

回答by Zack Katz

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

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

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

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

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

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

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

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

回答by YOU

if(window.opera){
    //do stuffs, for example
    alert(opera.version()); //10.10 
}

No kidding, there is an object operain opera browser.

别开玩笑了,operaopera 浏览器中有一个对象。

You may think, object operais overridable, but navigatoris overridable too.

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

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

我注意到,Opera 有 addEventListener 和 attachEvent,所以还有另一种方式,比如

if (window.addEventListener && window.attachEvent){
    //do stuffs, tested on opera 10.10
}

回答by kangax

In Prototype.js, we use this inference:

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

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

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

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

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.operareferencing that anchor element. There's your false positive...

说到无关的全局变量,记得在MSHTML DOM中,比如元素可以通过id/name全局解析;这意味着<a name="opera" href="...">foo</a>在标记中存在类似的东西将导致window.opera引用该锚元素。有你的误报...

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

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

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.

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

回答by Jonathan Marzullo

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

上述答案不再适用于新的 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) 
}

The new Opera 30release now fully uses Chromiumand also changed their userAgentto OPR

新的Opera 30版本现在完全使用Chromium并且将它们更改userAgentOPR

回答by Natrium

do you mind using jQuery?

你介意使用jQuery吗?

then you can use jQuery.browser(see documnentation)

然后你可以使用jQuery.browser见文档

But the jQuery-guys recommend not to use this.

但是 jQuery-guys 建议不要使用它。

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

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

Edit:

编辑:

For Mootools: use window.opera(see documentation)

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