How do I use JavaScript to detect what browser someone is using?

Omar Khan

Try this JavaScript code :-)


<SCRIPT LANGUAGE="JavaScript">
<!--
 beginRollover = false; // This handles a bug in Nav4.0x that executes the code too quickly.
 
 // convert all characters to lowercase to simplify testing 
 var agt=navigator.userAgent.toLowerCase();
  
 // *** BROWSER VERSION *** 
 // Note: On IE5, these return 4, so use is_ie5up to detect IE5. 
 var is_major = parseInt(navigator.appVersion); 
 var is_minor = parseFloat(navigator.appVersion); 
 
 var is_nav  = ((agt.indexOf('mozilla')!=-1) &&
 (agt.indexOf('spoofer')==-1) 
             && (agt.indexOf('compatible') == -1) &&
 (agt.indexOf('opera')==-1) 
             && (agt.indexOf('webtv')==-1));
              
 var is_nav2 = (is_nav && (is_major == 2)); 
 var is_nav3 = (is_nav && (is_major == 3)); 
 var is_nav4 = (is_nav && (is_major == 4)); 
 var is_nav6 = (is_nav && (is_major == 5));
  
 var is_ie   = (agt.indexOf("msie") != -1); 
 
 var is_ie3  = (is_ie && (is_major < 4)); 
 var is_ie4  = (is_ie && (is_major == 4) && (agt.indexOf("msie 5.0")==-1)
 ); 
 var is_ie5  = (is_ie && (is_major == 4) && (agt.indexOf("msie 5.0")!=-1)
 ); 
 
 var is_opera = (agt.indexOf("opera") != -1);
 var is_opera4 = (is_opera && (is_major == 4) && is_opera);
 
 function checkBrowser() {
 var version = "not recognised";
 if (is_nav2) {version = "Navigator 2";}
 else if (is_nav3) {version = "Navigator 3";}
 else if (is_nav4) {version = "Navigator 4";}
 else if (is_nav6) {version = "Navigator 6";}
 else if (is_ie3) {version = "Internet Explorer 3";}
 else if (is_ie4) {version = "Internet Explorer 4";}
 else if (is_ie5) {version = "Internet Explorer 5";}
 else if (is_opera4) {version = "Opera 4";}
 return version;
 }
 
 browserVersion= checkBrowser();
 
 alert("Your browser is: " + browserVersion);
 //-->

</SCRIPT>
0 Comments  (click to add your comment)
Comment and Contribute

 

 

 

 

 


(Maximum characters: 1200). You have 1200 characters left.

 

 

About | Sitemap | Contact