ATS Logo America's Town Square
Java Script Detection of Objects and Methods

Blue line

Tab A well-designed Java Script does one of two things:

  1. Performs the desired task without errors.
  2. Ignores the task if the desired functionality is not browser supported.

Simply stated, a Java Script applet should not choke, no matter what.

Avoidance is easy if the user's browser doesn't support Java Scripting. Java Script code is typically enclosed between the <SCRIPT></SCRIPT> tags. What to do when the desired effect -- such as image replacement -- is support in some browsers but not in others? If you're coding for your own amusement, you can simple say, "This works with Netscape 3.0 or later or Internet Explorer 4.0 or later. This is not at good idea. Anything producing a run time error should be avoided at all cost.

Here is one example -- from a magazine publisher's Web site -- to detect an image replacement browser.


  if (
     ( navigator.appName == "Netscape"
         && parseInt (navigator.appVersion) = 3 )
     ||
     ( navigator.appName == "Microsoft Internet Explorer"
         && parseInt (navigator.appVersion) = 4 )
     ||
         ) {
           version = "acceptable";
         } else {
           version="unknown";
     }

The code is cumbersome, even with a redundant test removed. In addition, it's error prone. The code only tests for Netscape Version 3.0 or later or Microsoft Internet Explorer Version 4.0 or above. What happens if a user has an unnamed -- image replacement capable -- browser? It doesn't pass the test.

Instead of testing for browser types, let's cut to the quick and test if the browser support Java Script's image object.


  // Does browser support image objects?
  AcceptableBrowser = document.images; 

AcceptableBrowser is functionally a Boolean variable. If the browser support the document.images object, it's set to true, otherwise false. Now just a simple conditional test.


  if (AcceptableBrowser) {
      JS Code;
   } // End of if (AcceptableBrowser)   

We could do it in one fell swoop.


  // Does browser support image objects?
  if (document.images) { 
      JS Code;
  } // End of if (document.images)   

I tend to like the first method since it's a little more self-documenting.

Summary

Instead of test for a particular flavor of browser, test to see if the desired object or method is supported. To illustrate:


  if (window.focus) {
      JS Code;
  } // End of if (window.focus)  

This example executes the JS code only if the user's browser supports the condition, in this case the focus method of the windows object.

Blue line

Other Tutorials

Please subscribe to the free ATS Newsletter so we can keep you
up to date with our "free stuff", special offers, and other goodies.
We will not supply your e-mail address to anyone else.
Your e-mail address:

Back to the ATS Home Page

Please sign our self-updating guestbook

How about some free stuff from ATS?

Please send our crotchety Webmaster some mail.
Copyright ©1995-98 America's Town Square
15 Hunter Drive
Tuckerton, NJ 08087
(609) 294-0320 [an error occurred while processing this directive]