Get a Quote Today!   |   + 1 (301) 424 3903




 
  Community
 
Need Help?
Visit our Training page
Request a Quote or Consultation
SIGN UP FOR THE TERATECH NEWSLETTER
 

ColdCuts

Cut: JavaScript Object Detection
Cut#: 15 
Cut Date: 01/20/1999 
Cutter: Douglas Smith 
Topic: JavaScript 
Overview: Here is a good article on using JS Object Detection instead of browser detection, to keep JavaScript from spiting out error messages when incompatibilites occur between browser versions.

http://www.webcoder.com/howto/article.html?number=14,length=4,demo=0,source=0

 

Cut Code:
A simple example of object detection in use can be found in the rollover script , which determines if
the browser supports the image object, thus allowing the images on the page to be controlled through
JavaScript. Here's the essence of how object detection is used in this script:

if (document.images) { // If the image object exists
// Preload images
}

function imgOn(imgName) {
if (document.images) { // If the image object exists
// Change images on the page
}
}

The first part of this script uses object detection to determine if the image object exists, and if so, it
creates and preloads images into memory. The second part defines a function which also detects the
image object, and if it exists, it changes the contents of the images on the page (when executed from
an onMouseOver event handler). As you can see, all of the code that depends on the image object
(preloading images and changing them on the page) lies safely within an if statement that first detects if
the image object is present. The basic syntax, to review, is as follows:

if (name of object you're detecting here) {
operations to inflict upon that object here