Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to determine the Opera browser using JavaScript

Tags:

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

like image 674
Avinash Avatar asked Jan 04 '10 08:01

Avinash


People also ask

How do you determine which browser supports a certain JavaScript feature?

The idea behind feature detection is that you can run a test to determine whether a feature is supported in the current browser, and then conditionally run code to provide an acceptable experience both in browsers that do support the feature, and browsers that don't.


2 Answers

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);

like image 108
Zack Katz Avatar answered Oct 26 '22 10:10

Zack Katz


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 } 
like image 23
YOU Avatar answered Oct 26 '22 11:10

YOU