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 && document.createComment
//Opera 6 and below
oldopera= window.opera && !document.createComment
Which presents a problem:
if(window.opera && (document.createComment || !document.createComment))
{
// Opera <= 7
}
else
{
// !(Opera <= 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 && !window.opera.addEventListener))
{
// Opera < 8
}
else
{
// !(Opera <
}
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 && !document.mimeType && !window.opera)
// opera < 8
|| (window.opera && !window.opera.addEventListener)
)
{
// put something here to deal with non-supported browsers
}
}
Popularity: 8% [?]