spacer

Webref WebRef   Sitemap · Experts · Tools · Services · Newsletters · About i.com

home / experts / javascript / column72


Netscape 6, Part I: Detection and Scripting

Developer News
News Flash: Adobe Has iPhone Workaround
Adobe's Flash 10.1 Goes Mobile (Minus iPhone)
A Salute to Visionary CEOs

Browser Detection

The most common pattern in your JavaScript scripts is probably the section that determines the browser type. And its structure is probably something like:

<SCRIPT LANGUAGE="JavaScript">
<!--
if (document.all) {
  alert("Internet Explorer Detected");
}
else if (document.layers) {
  alert("Netscape Navigator Detected");
}
else {
  alert("Unrecognized Browser Detected");
}
// -->
</SCRIPT>

Try this script. Convince yourself it's working as expected in Internet Explorer and in Netscape Navigator. Try now in Netscape 6. You should get the "Unrecognized Browser Detection" message. Netscape 6 does not support document.all, nor does it support document.layers. Netscape 6 supports document.getElementById. But Internet Explorer supports this method as well, so you need to be careful how to write your new browser sniffer. See Webreference's updated sniffer for example. The script above should look like this now:

<SCRIPT LANGUAGE="JavaScript">
<!--
if (document.all) {
  alert("Internet Explorer Detected");
}
else if (document.layers) {
  alert("Netscape Navigator Detected");
}
else if (document.getElementById) {
  alert("Netscape 6 Detected");
}
else {
  alert("Unrecognized Browser Detected");
}
// -->
</SCRIPT>

Try this script. Notice the positive identification of Netscape 6.

Next: How to write three-way browser-independent code

http://www.internet.com

internet.commediabistro.comJusttechjobs.comGraphics.com

Search:

WebMediaBrands Corporate Info

Legal Notices, Licensing, Reprints, Permissions, Privacy Policy.
Advertise | Newsletters | Shopping | E-mail Offers | Freelance Jobs

webref The latest from WebReference.com Browse >
Building a Banking Application Home Page with OOP · Mixing Scripting Languages · Review: phpFox, a Social Networking CMS with all the Bells and Whistles
Sitemap · Experts · Tools · Services · Email a Colleague · Contact FREE Newsletters 
 The latest from internet.com
Enterprise 2.0: Social Networking in the Cloud · BroadSoft Marketplace Hastens Pace of Telephony Innovation · Review: HTC Hero for Sprint


Produced by Yehuda Shiran and Tomer Shiran
All Rights Reserved. Legal Notices.
Created: December 4, 2000
Revised: December 4, 2000

URL: http://www.webreference.com/js/column72/3.html