Use the PrototypeJS Browser Detect
August 7th, 2008
Browser hacks are a necessary evil. In some areas, like css, exploiting other browser limitations is inevitable. In JavaScript, you can actually detect what a browser supports.
Prototype.js, trying to cover these browsers, has some solid detects in it.
Here is a snippet from Version 1.6.0.2:
Browser: {
IE: !!(window.attachEvent && !window.opera),
Opera: !!window.opera,
WebKit: navigator.userAgent.indexOf('AppleWebKit/') > -1,
Gecko: navigator.userAgent.indexOf('Gecko') > -1 && navigator.userAgent.indexOf('KHTML') == -1,
MobileSafari: !!navigator.userAgent.match(/Apple.*Mobile.*Safari/)
}
If you need a browser detect for some reason, you can access the result from your own scripts. For example: I needed a radio button that reacted to selection by triggering another function,and IE triggers the ‘onChange’ event at a later point than all the other browsers:
<input type='radio' id='my_radio' />
...
<script type='text/javascript'>
//<![CDATA[
var signal = Prototype.Browser.IE ? 'focus' : 'change';
Event.observe('my_radio',signal,function(){
react();
});
//]]>
</script>
Posted in JavaScript |