25

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

Avinash
  • 6,064
  • 15
  • 62
  • 95
  • Why do you need to detect Opera? And, from your question, it seems that you want to detect users without JS... which is kinda impossible using JS. – James Jan 04 '10 at 10:36
  • i need to call one js function only if there is browser is opera thats whay i am here...... – Avinash Jan 04 '10 at 12:00
  • 2
    Whenever possible, try to detect the problem and not the browser. I admit that this is sometimes hard :( – hallvors Jan 04 '10 at 13:53
  • 3
    @Avinash: the reason J-P asked the question is that we didn't know why you're checking browser and after that response, we still don't. if you're checking because you want to know that a certain feature is supported (which is quite often why one checks for a specific browser), you should try to always check for *that feature* rather than the browser. If you're thinking "function *x* only exists in opera, so i'll only call that when the user runs opera", well, what happens if that function is removed in later versions of opera? rather: check *if function x exists, use function x* – David Hedlund Jan 04 '10 at 14:33
  • Hi Avinash, would you consider changing the accepted answer? Opera changed their userAgent string, the solution on that answer ain't working anymore... – brasofilo Jan 27 '19 at 20:29

6 Answers6

40

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

Zack Katz
  • 1,310
  • 11
  • 9
18
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
}
Dan
  • 55,715
  • 40
  • 116
  • 154
YOU
  • 120,166
  • 34
  • 186
  • 219
  • 3
    @Justin, this isn't feature detection. If anything, it's feature-based browser detection. See http://www.nczonline.net/blog/2009/12/29/feature-detection-is-not-browser-detection/ ... Also `window.opera && Object.toString.call(window.opera) == "[object Opera]"` would be a more solid check. – James Jan 04 '10 at 10:33
  • Thanks J-P, but `Object.toString.call(window.opera)` does not work in my opera 10.10, so I updated mine to working one. – YOU Jan 04 '10 at 10:40
  • 1
    Sorry @S.Mark, I should have written `Object.prototype.toString.call(window.opera)` – James Jan 04 '10 at 10:43
  • 1
    @J-P: in what cases is it desirable to use `Object.prototype.toString.call` rather than calling `toString` on the object? (given that we've checked for null immediately before that) Is it in case `window.opera` should implement its own `toString` function? – David Hedlund Jan 04 '10 at 11:13
  • 1
    @David, yep, exactly that. It's not really necessary -- just makes the check a little more reliable. – James Jan 04 '10 at 13:51
  • 5
    Opera 16 doesn't have `window.opera` any more – nickf Sep 13 '13 at 11:57
  • `(window.addEventListener && window.attachEvent)` isn't a good test for Opera since the condition is also fulfilled by IE9. – craigpatik Dec 20 '13 at 17:12
9

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

Jonathan Marzullo
  • 6,879
  • 2
  • 40
  • 46
7

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.

kangax
  • 38,898
  • 13
  • 99
  • 135
4

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

navigator.userAgent.indexOf("Opera");
David Hedlund
  • 128,221
  • 31
  • 203
  • 222
  • 1
    You should use feature detection where ever possible. See @S.Mark's answer. – Justin Johnson Jan 04 '10 at 10:16
  • 5
    I agree that you should use feature detection wherever possible, but this is still a correct answer to the explicit question of *how to determine if a client is running opera*. If the question was *how to determine if this opera-specific function exists* then checking for that function would be preferable. S.Mark's suggestion, while quite convenient (I upvoted it, too), is hardly feature detection at all. It relies on an object that's only present in opera, and checks for that, yes, but it doesn't check for the *specific* feature (unknown to us) that underlies the opera-check-requirement. – David Hedlund Jan 04 '10 at 10:24
  • 5
    Justin... The navigator string is designed for browser detection. Using feature-detection to detect browsers is even worse than using plain browser detection. – James Jan 04 '10 at 10:34
  • 10
    This method doesn't work anymore, because Opera switched to the render-engine of Chrome. That means navigator.userAgent delivers something like this: "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/28.0.1500.95 Safari/537.36 OPR/15.0.1147.153" Note, that there is no "Opera", so the result is always -1. Either identify Opera by feature detection or use the method of @Zack Katz below. http://stackoverflow.com/a/17436191/1058552 – Daniel Blaichinger Aug 21 '13 at 11:05
  • 2
    Doesn't work anymore. Use: isOpera() {return (navigator.userAgent.indexOf("OPR") !== -1)}; – lukyer Jul 25 '16 at 12:39
  • No longer valid! – zardilior Sep 05 '20 at 15:44
  • Your reply is not useful as of 2021. Whether i run your code snippet in Opera, or Chrome, i get -1. – Dennis Kozevnikoff Aug 18 '21 at 15:14
-1

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)

Natrium
  • 30,772
  • 17
  • 59
  • 73
  • i am using mootools 1.2 , so how to do that in mootools1.2 ? – Avinash Jan 04 '10 at 08:58
  • I edited my post, but imho this can be found very easy with google (like I did) – Natrium Jan 04 '10 at 09:44
  • -1 Why suggest something that even the makers suggest against? – Justin Johnson Jan 04 '10 at 10:17
  • because that was the Avinash asked for, how to detect the user is using opera or not. – Natrium Jan 04 '10 at 10:32
  • 4
    @Justin: The makers don't suggest against using `jQuery.browser` to find out what browser the client is running, they suggest against using `jQuery.browser` to check for specific feature support. The method does exactly what the OP requested; Natrium provided an answer that delivered what was asked for, while noting that there are times when it is not desirable to do what was asked for (we don't have enough info on what Avinash is working on to know whether or not an explicit browser-check is agreeable in that scenario). I'd remove the downvote if I were you, this is quite valid. – David Hedlund Jan 04 '10 at 11:00