Skip to Content Skip to Search Go to Top Navigation Go to Side Menu
Search on Site

Detecting Opera

Monday, April 10, 2006

A.K.A. - You can run, but you can’t hide

Mmh, object detection. Assuming JavaScript is turned on (a rather large caveat, granted), it seems to be a pretty reliable method of figuring out which browser your user is, uh, using.

Much like you can code CSS for certain browsers by exploiting holes in their support for various bits any bobs, different browsers support different bits of the ECMAScript standard, and other add their own extensions into the mix.

There’s a wonderful page on object detection over at dhtmlnirvana.com (don’t be afraid of the name), but there is one problem. If you are piddling about with AJAX, you’ll need some way of distinguishing between Opera >= 8 (which supports xmlhttprequest) and Opera < 8 (which doesn’t).

The dhtmlnirvana page has the following:

//Opera 7
opera7 = window.opera &amp;&amp; document.createComment

//Opera 6 and below
oldopera= window.opera && !document.createComment

Which presents a problem:

if(window.opera &amp;&amp; (document.createComment || !document.createComment))
{
// Opera &lt;= 7
}
else
{
// !(Opera &lt;= 7)
}

which boils down to:

if(window.opera)
{

}

neatly ghettoising all those poor Opera users.

Thankfully, some helpful chap (or chapess, it doesn’t specify) has put a page up on the window.opera object (an object provided by Opera for fun things like JavaScript debugging), and first on the list?

# opera.addEventListener(string: type,function: handler,bool: phase)

* Opera 5&6&7-7.6: No support
* Opera 8+: Registers an event listener for a User JavaScript event. This method only has any effect in User JavaScript files.

Genius. Now we can use:

if((window.opera &amp;&amp; !window.opera.addEventListener))
{
// Opera &lt; 8
}
else
{
// !(Opera &lt; 8)
}

Throw in some other bits and bobs and you have:

function checkAJAXCapable()
{
if( // non dom browsers
(!document.getElementsByTagName || !document.getElementById)
// ie 5/mac
|| (document.all &amp;&amp; !document.mimeType &amp;&amp; !window.opera)
// opera &lt; 8
|| (window.opera &amp;&amp; !window.opera.addEventListener)
)
{
// put something here to deal with non-supported browsers
}
}

Popularity: 9% [?]

Leave a Reply


In order to submit a comment, you need to mention your name and your email address (which won't be published).

Comment Form