Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Opera browser identification

Opera browser

Opera browser identification

by  starway  Posted    (Edited  )
Opera browser is known for it's ability to "spoof" browser name detection when [tt]navigator.appName[/tt] property is used. It's not some "hidden" function but one of common preferences that any user can change by his wish. For example, user of the latest official Opera 6.0 release has several default options of it's browser identification. The options are as follows:[ul][li]Identify as Opera
[li]Identify as Mozilla 5.0
[li]Identify as Mozilla 4.76
[li]Identify as Mozilla 3.0
[li]Identify as MSIE 5.0 (default)
[/ul]If you try to detect the browser used by your visitor by checking the value of [tt]navigator.appName[/tt] you may be wrong - in Opera case you can get any of above mentioned ones. But it's still Opera!

There are several ways to detect the real values, one of them is checking the value of [tt]navigator.userAgent[/tt]. Here what I got using Opera 6.0 on Win2k system:

Id as Mozilla 5.0: Mozilla/5.0 (Windows 2000; U) Opera 6.0 [en]

Id as Opera: Opera/6.0 (Windows 2000; U) [en]

Id as MSIE 5.0: Mozilla/4.0 (compatible; MSIE 5.0; Windows 2000) Opera 6.0 [en]


You may see that each string contains "Opera" substring, that tells you exactly what browser are you dealing with. Also, there's an option to see what type is Opera "pretending" to be at this moment. [tt]navigator.appName[/tt] is good enough for this. We can perform some check-ups like these:

if ( navigator.userAgent.indexOf("Opera") !=-1 ) // this is Opera

if (( navigator.userAgent.indexOf("Opera") !=-1 ) && ( navigator.userAgent.indexOf("MSIE") !=-1)) //this is Opera identified as IE

if (( navigator.userAgent.indexOf("Opera") !=-1 ) && ( navigator.appName == "Opera" )) // pure Opera!

The results will give us the full picture.
There's also another way to find out whether it is Opera or not. Check the folowing:

window.opera

Of course, it will be true only in Opera case. The use of this property of window object in Opera browser let us know what we deal with (without getting into details):
var opera = (window.opera ? true : false)

In order to know other details, like what Opera tries to tell you about itself, you have to combine these sample codes for [tt]window.opera[/tt] and [tt]navigator.userAgent[/tt] mentioned above.

Opera is very easy to use for development as it's also known for various standards compliance. But there are differences between it and other browsers, and sometimes it's important to use them.

Hope this will help you to develop more cross-browser websites.
Good luck.
_______
starway@operamail.com
Register to rate this FAQ  : BAD 1 2 3 4 5 6 7 8 9 10 GOOD
Please Note: 1 is Bad, 10 is Good :-)

Part and Inventory Search

Back
Top