views:

210

answers:

4

Hi All,

i want to determine that the browser of the client machines in opera or not using javascript.

Thanks in advance

Avinash

+1  A: 

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

navigator.userAgent.indexOf("Opera");
David Hedlund
You should use feature detection where ever possible. See @S.Mark's answer.
Justin Johnson
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
Justin... The navigator string is designed for browser detection. Using feature-detection to detect browsers is even worse than using plain browser detection.
J-P
A: 

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
i am using mootools 1.2 , so how to do that in mootools1.2 ?
Avinash
I edited my post, but imho this can be found very easy with google (like I did)
Natrium
-1 Why suggest something that even the makers suggest against?
Justin Johnson
because that was the Avinash asked for, how to detect the user is using opera or not.
Natrium
@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
+3  A: 
if(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 (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
}
S.Mark
+1 for feature detection
Justin Johnson
J-P
Thanks J-P, but `Object.toString.call(window.opera)` does not work in my opera 10.10, so I updated mine to working one.
S.Mark
Sorry @S.Mark, I should have written `Object.prototype.toString.call(window.opera)`
J-P
@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
@David, yep, exactly that. It's not really necessary -- just makes the check a little more reliable.
J-P
+2  A: 

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