Go4Expert

Go4Expert (http://www.go4expert.com/)
-   JavaScript and AJAX (http://www.go4expert.com/articles/javascript-tutorials/)
-   -   Detecting Browsers with JavaScript (http://www.go4expert.com/articles/detecting-browsers-javascript-t1911/)

pradeep 16Nov2006 14:31

Detecting Browsers with JavaScript
 
Many times we need to write certain parts of our script according to the browser, i.e. we need to detect the browser. Here's a snippet of how to do it.

Code: JavaScript

var userAgent = navigator.userAgent.toLowerCase();
var is_opera  = (userAgent.indexOf('opera') != -1);
var is_saf    = ((userAgent.indexOf('applewebkit') != -1) || (navigator.vendor == "Apple Computer, Inc."));
var is_webtv  = (userAgent.indexOf('webtv') != -1);
var is_ie     = ((userAgent.indexOf('msie') != -1) && (!is_opera) && (!is_saf) && (!is_webtv));
var is_ie4    = ((is_ie) && (userAgent.indexOf("msie 4.") != -1));
var is_moz    = ((navigator.product == 'Gecko') && (!is_saf));
var is_kon    = (userAgent.indexOf('konqueror') != -1);
var is_ns     = ((userAgent.indexOf('compatible') == -1) && (userAgent.indexOf('mozilla') != -1) && (!is_opera) && (!is_webtv) && (!is_saf));
var is_ns4    = ((is_ns) && (parseInt(navigator.appVersion) == 4));


Bhullarz 17Apr2007 23:46

Re: Detecting Browsers with JavaScript
 
how to embed this in html page

pradeep 18Apr2007 10:01

Re: Detecting Browsers with JavaScript
 
Inside a <script> tag of course, or you can put it in a external javascript file and call that file in your HTML page.


All times are GMT +5.5. The time now is 15:53.