spacer

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

home / experts / javascript / column6


Object Detection

Developer News
Metasploit 3.2 Offers More 'Evil Deeds'
'Thank You Apple. Seriously.'
The Buzz: BlackBerry App Store Seen Next

Browser detection is not recommended because it checks what browser the user is running, not what features are supported. So if your script depends on browser detection to decide whether or not to execute a specific function or script segment, it might not work with future browers. The number of browsers and browser versions is constantly growing, and so is the number of inconsistent features. Furthermore, you must be a JavaScript hacker to know exactly what features are supported by each browser. Is window.onerror supported by Netscape Navigator 2.0? How about Microsoft Internet Explorer 3.0? With object detection you don't need to know!

As you already know, if you refer to an object (or a property, method, function, etc.) that is not supported, the browser cannot execute the script, and it displays a very annoying error message. Object detection simply checks if an object exists before you use it in the script. We used object detection in our column "Universal JavaScript Rollovers" to find out if the browser supports the document.images object before using it to swap the desired image:

if (document.images) {
  // rollover script here
}

The general syntax for object detection is:

if (the name of the required object) {
  // statements that use that object
}

A few restrictions apply to the object in the conditional statement:

  • It cannot be specified as a top-level object. For example, you must use window.onerror instead of onerror, even though they have the same meaning.
  • Its parent object must be specified, and must exist. For example, you should not use document.images.length, because the browser might not support the parent object, document.images. If you need to detect a nested object, you can take advantage of JavaScript's short-circuit evaluation, as in:

    if (document.images && document.images.length) {
      // additional statements here
    }

    When you combine more than one conditional expression with a Boolean "and" operator (&&), the JavaScript interpreter stops evaluating the entire condition when one of the sub-expressions evaluates to false. In this case, if document.images evaluates to false (meaning that it does not exist), the interpreter does not continue, so document.images.length is not evaluated (thus, no error is generated if the browser does not support the parent object, document.images).

We all know that an if statement requires a Boolean value. So why does it work with objects? The answer is very simple. An object that does not exist has a null value, which is a special one, because it converts to false when used in a statement that requires a Boolean expression.

http://www.internet.com



JupiterOnlineMedia

internet.comearthweb.comDevx.commediabistro.comGraphics.com

Search:

Jupitermedia Corporation has two divisions: Jupiterimages and JupiterOnlineMedia

Jupitermedia Corporate Info


Legal Notices, Licensing, Reprints, & Permissions, Privacy Policy.

Advertise | Newsletters | Tech Jobs | Shopping | E-mail Offers

Solutions
Whitepapers and eBooks
IBM Whitepaper: Innovative Collaboration to Advance Your Business
Internet.com eBook: Real Life Rails
Avaya Article: Call Control XML - Powerful, Standards-Based Call Control
Tripwire Whitepaper: Seven Practical Steps to Mitigate Virtualization Security Risks
Internet.com eBook: The Pros and Cons of Outsourcing
Go Parallel Article: Scalable Parallelism with Intel(R) Threading Building Blocks
Internet.com eBook: Best Practices for Developing a Web Site
IBM CXO Whitepaper: The 2008 Global CEO Study "The Enterprise of the Future"
Avaya Article: Call Control XML in Action - A CCXML Auto Attendant
Go Parallel Article: James Reinders on the Intel Parallel Studio Beta Program
IBM CXO Whitepaper: Unlocking the DNA of the Adaptable Workforce--The Global Human Capital Study 2008
Adobe Acrobat Connect Pro: Web Conferencing and eLearning Whitepapers
Go Parallel Article: Getting Started with TBB on Windows
HP eBook: Storage Networking , Part 1
MORE WHITEPAPERS, EBOOKS, AND ARTICLES
Webcasts
Go Parallel Video: Intel(R) Threading Building Blocks: A New Method for Threading in C++
HP Video: Is Your Data Center Ready for a Real World Disaster?
Microsoft Partner Portal Video: Microsoft Gold Certified Partners Build Successful Practices
HP On Demand Webcast: Virtualization in Action
Go Parallel Video: Performance and Threading Tools for Game Developers
Rackspace Hosting Center: Customer Videos
Intel vPro Developer Virtual Bootcamp
HP Disaster-Proof Solutions eSeminar
HP On Demand Webcast: Discover the Benefits of Virtualization
MORE WEBCASTS, PODCASTS, AND VIDEOS
Downloads and eKits
Microsoft Download: Silverlight 2 Software Development Kit Beta 2
30-Day Trial: SPAMfighter Exchange Module
Red Gate Download: SQL Toolbelt
Iron Speed Designer Application Generator
Microsoft Download: Silverlight 2 Beta 2 Runtime
MORE DOWNLOADS, EKITS, AND FREE TRIALS
Tutorials and Demos
IBM IT Innovation Article: Green Servers Provide a Competitive Advantage
Microsoft Article: Expression Web 2 for PHP Developers--Simplify Your PHP Applications
Featured Algorithm: Intel Threading Building Blocks - parallel_reduce
MORE TUTORIALS, DEMOS AND STEP-BY-STEP GUIDES
webref The latest from WebReference.com Browse >
Popular JavaScript Framework Libraries: An Overview · Controllers: Programming Application Logic - Part 2 · How to Use JavaScript to Validate Form Data
Sitemap · Experts · Tools · Services · Email a Colleague · Contact FREE Newsletters 
 The latest from internet.com
Choosing the Right Online Backup Provider · Mother Avaya Nurtures Her Technology Partners · Software as a Service a Winning Model for Hotspot Provider

Created: November 4, 1997
Revised: December 4, 1997
URL: http://www.webreference.com/js/column6/object.html