PDA

View Full Version : [JAVASCRIPT] Sniff Opera



molendijk
05-31-2008, 08:38 PM
1) CODE TITLE: Sniff Opera

2) AUTHOR NAME/NOTES: Arie Molendijk

3) DESCRIPTION: You can sniff Opera through if(history.navigationMode) {bla}.

I had my doubts about posting this, but a quick search on Google told me that very few coders seem to use it (I found 2 entries only). So this might be a useful (unknown?) piece of code for some of you. If not, tell me, and I'll withdraw the thread.

jscheuer1
06-01-2008, 12:36 AM
Opera has its own proprietary window object:

window.opera

That's why no one in their right mind would ever look any farther than that for sniffing Opera.

Usage:


if (window.opera) {
do opera stuff here
}

molendijk
06-01-2008, 09:17 AM
John, I recall having read somewhere that older versions of Opera do not support 'if (window.opera)'. As history.navigationMode has been part of Opera all along, I thought that 'if(history.navigationMode)' would be better. But I may very well be wrong on that.
---
Arie.

jscheuer1
06-01-2008, 11:04 AM
Sniffing is generally a bad idea however it its done. But when another way cannot be found, it is better than nothing until a better way is devised.

That said, sniffing for Opera using that older method (if it is as you say, applicable to more versions) would be like sniffing for FF/Netscape using if(navigator.appName=='Netscape'). The number of browsers selected would be so great as to have no practical use for most code branching. It would include at least NN 4 up to the most recent FF release I've seen, the two of which share almost nothing else in common.

They do both do window.innerWidth I believe. But there are so many others that do as well. At that point, it would be much better to test for the property.

I'm sure it is much like that with early Opera, and more recent versions. Even those browsers which return true for window.opera have a fairly wide range of feature sets.

So, although what you've identified may have some utility, it is probably much less than you might imagine. Its greatest value would be in determining whether or not history.navigationMode could be used, that is if that is anything different than history.go. Is it? I mean aside from being a possibility for sniffing, does it do anything?

molendijk
06-01-2008, 11:40 AM
So, although what you've identified may have some utility, it is probably much less than you might imagine. Its greatest value would be in determining whether or not history.navigationMode could be used, that is if that is anything different than history.go. Is it? I mean aside from being a possibility for sniffing, does it do anything?
When you navigate with the Back and Forward buttons, Opera doesn't fire any load and unload events, because it fetches previous pages from its cache. Using
history.navigationMode="compatible" instead of
history.navigationMode="fast" or
history.navigationMode="automatic" (or instead of doing nothing) solves that problem.
---
Arie.

jscheuer1
06-01-2008, 11:56 AM
Yes, I just Googled it while you were posting, of course one would need to:


if (history.navigationMode)
history.navigationMode = 'compatible';

or it could create more problems than it would solve. Here is where that 'sniff' should be used, as it's now feature detection, not browser sniffing. In fact, it would generally be preferable to:


if (window.opera)
history.navigationMode = 'compatible';

The one (window.opera) having no direct relation to the other (history.navigationMode). It would work out in most if not all modern cases, but only due to coincidence.

molendijk
06-01-2008, 12:21 PM
... of course one would need to:

if (history.navigationMode)
history.navigationMode = 'compatible';
or it could create more problems than it would solve. Here is where that 'sniff' should be used, as it's now feature detection, not browser sniffing.
You're right!
---
Arie.