Detecting Browsers with JavaScript

pradeep's Avatar author of Detecting Browsers with JavaScript
This is an article on Detecting Browsers with JavaScript in JavaScript and AJAX.
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's Avatar
Skilled contributor
how to embed this in html page
pradeep's Avatar, Join Date: Apr 2005
Team Leader
Inside a <script> tag of course, or you can put it in a external javascript file and call that file in your HTML page.