Hello and welcome to the Webmasters Forums!. This is the best place to get webmasters resources for free. Get $2 for free today, read more - Make your payment today. Download premium and professional templates for free. Get free web hosting without ads, read more. You can get lot more by simply join with this forum. To gain full access to the forums you must sign up for a free account.


Post Reply  Post Thread 

JavaScript Browser Detection

Post Bank
Posting Manager
******

Posts: 995
Group: Forum Team
Joined: Sep 2006
Status: Online
Make money from now. You can make money just for posting on this forum. Every discussions on this community gives you more money. $2 minimum payout. So get your payment today, SignIn with this forum.

Signin to Remove this Post

ivenms
Administrator
*******


Posts: 2,179
Group: Administrators
Joined: Sep 2006
Status: Offline
Reputation: 14
Points: 4389 (Donate)
Post: #1

Big Grin JavaScript Browser Detection


Browser detection allows you to find out what browser your viewer is using, and then perform a script based on it-- or just to send a friendly message to those with your favorite browser.

There are two objects often used for this, the navigator.appName and navigator.appVersion objects. The first one returns the name of the browser, the second returns the version of the browser.

If the browser is Netscape, navigator.appName returns the string "Netscape". If it is Internet Explorer, it returns the string "Microsoft Internet Explorer". Using just this, you could make a script to alert people as to what browser they are using (just to bug them). Like this:


<HEAD>
<SCRIPT language="JavaScript">
<!--
var browserName=navigator.appName;
if (browserName=="Netscape")
{
alert("Hi Netscape User!");
}
else
{
if (browserName=="Microsoft Internet Explorer")
{
alert("Hi, Explorer User!");
}
else
{
alert("What ARE you browsing with here?");
}
}
//-->
</SCRIPT>
</HEAD>

You can do the same thing with the navigator.appVersion, except you will most likely want to grab just the integer from the version information (2,3,4, etc.). To do this, we use the parseInt() function:


var browserVer=parseInt(navigator.appVersion);

Now, it returns only the integer value and not something like version 4.51. It just sends back 4 in that case. Thus, we could alert viewers as to whether their browser is new enough for us or not:


<HEAD>
<SCRIPT language="JavaScript">
<!--
var browserVer=parseInt(navigator.appVersion);
if (browserVer >= 4)
{
alert("Your browser is new enough for my site.");
}
else
{
alert("I think you need a new browser!");
}
//-->
</SCRIPT>
</HEAD>

Of course, you can use both objects to be more exact. You could look for a certain set of browsers and only if they are above a certain version:


<HEAD>
<SCRIPT language="JavaScript">
<!--
var browserName=navigator.appName;
var browserVer=parseInt(navigator.appVersion);
if ((browserName=="Netscape" && browserVer>=3) || (browserName=="Microsoft Internet Explorer" && browserVer>=4))
version="n3";
else
version="n2";

if (version=="n3")
alert("Your browser passes the test");
else
alert("You need an upgrade, I think.");
//-->
</SCRIPT>
</HEAD>

One of the more common uses for browser detection is to redirect a viewer to a page made for his/her browser type or browser version. We will discuss that in the next section on redirection.


Read: General Rules & Policies before posting.
Make Money By Posting | Earning and Exchanging Points | Add Your Links
15-09-2006 03:20 AM
Find all posts by this user Quote this message in a reply
Post Reply  Post Thread 

Possibly Related Threads...
Thread: Author Replies: Views: Last Post
Smile Learn JavaScript Basics ivenms 1 656 14-07-2008 06:20 PM
Last Post: ratedguy
Toungue Image Rollover Script Javascript ratedguy 0 48 14-07-2008 06:15 PM
Last Post: ratedguy
  I'm New to Javascript MaGiC DaN90 3 898 16-05-2008 04:37 PM
Last Post: ivenms
  Using iframes with javascript godkillah 0 1,387 26-01-2008 06:19 AM
Last Post: godkillah
  The JavaScript that Randomly Select the Quotes ivenms 9 601 29-09-2007 01:46 AM
Last Post: godkillah
Rolleyes JavaScript Redirection ivenms 0 620 15-09-2006 03:21 AM
Last Post: ivenms
Smile JavaScript Alerts ivenms 0 631 15-09-2006 03:04 AM
Last Post: ivenms

View a Printable Version
Send this Thread to a Friend
Subscribe to this Thread | Add Thread to Favorites
Rate This Thread:

Forum Jump:

Sign In to Remove Ads

Download 1000's of web templates. Unlimited access!
World's Best Web Hosting
Website of the Month

Create-a-Page for Free
SOTM June 2008


Accepting Submissions
for July 2008
Resources

Recommended Sites:



Visit our Sponsors!

Current time: 30-08-2008, 07:42 AM


Copyright © 2002-2008 MyBB Group
Powered By MyBB