There are many reasons why you might need to use a detection script. You might need to create separate stylesheets depending on what browser or operating system is used. You might want to use a browsers enhanced features without breaking your page on other browsers.
Personally I think things need to be kept in perspective. There are differences in how webpages render between operating systems browsers even between different releases of the same version.
So, basically all we really need to detect is the operating system and the browser. Rendering and scripting differences still exist across browsers/operating systems now but they are less severe than what they were a few years ago.
Using the code below we can detect main operating systems and the most common browsers (you can add more if you wish, if your needs aren't met).
<script language="JavaScript" type="text/javascript">
<!--
//Platform
var Mac_OS = (navigator.userAgent.indexOf("mac") != -1) || (navigator.userAgent.indexOf("Mac") != -1);
var Linux_OS = (navigator.userAgent.indexOf("Linux") != -1);
var Win_OS = (navigator.userAgent.indexOf("Win") != -1);
//if the OS isn't win mac or linux
var Other_OS = (!Win_OS && !Linux_OS && !Mac_OS != -1);
//Browsers
var Opera = (navigator.userAgent.indexOf('Opera') != -1);
var MSIE = (navigator.userAgent.indexOf('MSIE') != -1);
var Moz = (navigator.userAgent.indexOf('Gecko') != -1);
var NS = (navigator.userAgent.indexOf('Netscape') != -1 && navigator.userAgent.indexOf('Gecko') != -1);
if(document.layers){var Nav4="true";};
var Konqueror = (navigator.userAgent.indexOf('Konqueror') != -1);
//Opera workaround - default install of opera identifies as both opera and msie
if (Opera && MSIE || Opera){var Opera="true"; MSIE="false";}
if (MSIE && !Opera){MSIE="true";}
//if its some other browser
var Other_Browser = (!Opera && !MSIE && !Moz && !NS && !Nav4 && !Konqueror != -1);
// -->
</script>
Mac_OS | |
Linux_OS | |
Win_OS | |
Other_OS | |
Opera | |
MSIE | |
Moz | |
NS | |
Nav4 | |
Konqueror | |
Other_Browser |