AlfieWebDevTips Homefor new and experienced developers
[Home Home] [Web Development Web Development ] [Code Generators Code Generators ] [Resources Resources ] [Contact Us Contact ] |WebDevTips Privacy Privacy |
Print This Page

You are here : Home - Web Development - Javascript - Browser Detect

Browser Detect



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>


Here are the values returned from the script.
Mac_OS
Linux_OS
Win_OS
Other_OS
Opera
MSIE
Moz
NS
Nav4
Konqueror
Other_Browser


Now that you have the values you can manuiplate them however you want. I won't go into anymore detail as you now have the bones to do with as you wish but with a little bit of manipulation I can tell you that you are browsing using






Google
 

[back to top of page]   

[labelled with icra -  - this link will open in a new window] [RSS Content]

[Copyright © WebDevTips]