News:

This week IPhone 15 Pro winner is karn
You can be too a winner! Become the top poster of the week and win valuable prizes.  More details are You are not allowed to view links. Register or Login 

Main Menu

Browser detection using JavaScript - Tip JavaScript

Started by ben2ong2, October 04, 2006, 03:56:49 AM

Previous topic - Next topic

0 Members and 7 Guests are viewing this topic.

ben2ong2


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.]




You are not allowed to view links. Register or Login
You are not allowed to view links. Register or Login