Java Reference
In-Depth Information
// our code using the document.all collection
}
the if statement's condition will evaluate to true if the property returns a valid value. If the property
is not supported, its value will be undefined , and the if statement will evaluate to false .
To check whether a particular method is supported, you can do the following:
if (document.getElementById)
{
// code using document.getElementById()
}
else
{
// code for browsers that do not have that method
}
You've “tested” the existence of the method as you did with properties. Just remember not to include
the opening or closing parentheses after the method even if it normally has a number of parameters.
The getElementById method, for example, has one parameter, and you'll look at it in Chapter 12.
Functions (and methods) are actually objects in the JavaScript language. While this advanced topic
isn't covered in this topic, Professional JavaScript for Web Developers by Nicholas Zakas (published
by Wrox) provides an in-depth discussion on the topic.
The next example shows how to use object checking to ensure that you execute the right code for the
right browser; this technique is not foolproof but can be very useful.
Try It Out
Checking for Supported Browser Properties
<!DOCTYPE html PUBLIC “-//W3C//DTD XHTML 1.0 Strict//EN”
“http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd”>
<html xmlns=”http://www.w3.org/1999/xhtml”>
<head>
<title>Chapter 6: Example 6</title>
</head>
<body>
<script type=”text/javascript”>
var browser = “Unknown”;
var version = “0”;
if (window.opera)
{
browser = “Opera”;
version = “5+”;
if (window.opera.setPreference)
{
version = “9”;
}
}
Search WWH ::




Custom Search