Ryan's District Boards

Computer, programming, and webmaster help , support , tips and tricks => Tutorials Zone! => Internet webmaster computer programming technology tips and tricks => Java Script Tutorials => Topic started by: ben2ong2 on October 04, 2006, 03:56:49 AM

Title: Browser detection using JavaScript - Tip JavaScript
Post by: ben2ong2 on October 04, 2006, 03:56:49 AM

With a little help from JavaScript, you can easily detect the browser used by visitors on your web site. Browser detection has several uses. It helps you analyze your visitors if you store this information in a database, which assists you in building visitor profiles for better web site maintenance. You can redirect visitors to browser specific pages or display browser specific content such as style sheets.

The code described in this tip will concentrate on Netscape and Internet Explorer since they share more than 90% of the browser market. However, the code can be extended to detect other browsers.

<SCRIPT LANGUAGE="JavaScript" TYPE="TEXT/JAVASCRIPT">
<!--

function whichname()
   {
   var bname = navigator.appName;
   alert("You are using " + bname);
   }

//-->
</SCRIPT>
Clicking here will display your browser name
The crux of this code is the appName property of the navigator object. This property contains the name of the browser. We assign this to a variable bname and then display it using the alert method. (Simple enough?)
[Note: Our code resides in a function, which makes it possible to call it many times on a page. For example, I called this function using onLoad() in the body tag and again using onClick() in <A> for the link above.]