如何使用 JavaScript 确定 Opera 浏览器
我想使用JavaScript判断客户端机器的浏览器是否是Opera,该怎么做?
I want to determine whether the browser of the client machines is Opera using JavaScript, how to do that?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
navigator
对象包含您需要的所有信息。这应该做:The
navigator
object contains all the info you need. This should do:你介意使用 jQuery 吗?
那么你可以使用
jQuery.browser
(参见文档)但 jQuery 专家建议不要使用它。
编辑:
对于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.
Edit:
For Mootools: use
window.opera
(see documentation)现在 Opera 使用 Chrome 渲染引擎,公认的解决方案不再有效。
用户代理字符串显示如下:
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:
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);
不开玩笑,opera浏览器中有一个对象
opera
。您可能会认为,对象
opera
是可重写的,但navigator
也是可重写的。更新:
为了获得更准确的结果,你可以这样做
我注意到,Opera都有addEventListener和attachEvent,所以还有另一种方法,例如
No kidding, there is an object
opera
in opera browser.You may think, object
opera
is overridable, butnavigator
is overridable too.UPDATE:
To get more accurate result, you could do like
And I noticed, Opera have both addEventListener and attachEvent, so there is also another way like
上述答案在新的 Opera 30 中不再有效。因为 Opera 现在使用 Chromium。请使用以下内容:
新的 Opera 30 版本现已完全使用 Chromium,并将其
userAgent
更改为 OPRThe above answers no longer work in the new Opera 30. Since Opera now use Chromium. Please use the below:
The new Opera 30 release now fully uses Chromium and also changed their
userAgent
to OPR在 Prototype.js 中,我们使用这个推论
:本质上是检查 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:
This essentially checks that
window.opera
object exists and its internal [[Class]] value is "Opera". This is a more solid test than just checking forwindow.opera
existence, since there's much less chance of some unrelated globalopera
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 inwindow.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.