11.12.2012 Views

JavaScript 2.0-The Complete Reference, Second ... - freecodingtutorial

JavaScript 2.0-The Complete Reference, Second ... - freecodingtutorial

JavaScript 2.0-The Complete Reference, Second ... - freecodingtutorial

SHOW MORE
SHOW LESS

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

to detect if Internet Explorer is in use. However, does the existence of document.all really<br />

mean that Internet Explorer is in use? <strong>The</strong> truth of the matter is that another browser could<br />

support document.all but not necessarily provide all the features found in Internet Explorer.<br />

<strong>The</strong> developer might even be simulating document.all with their own code. Given all the<br />

possibilities for trouble, it might be better to check for each object specifically, so instead we<br />

might use<br />

var allObject = (document.all) ? true : false;<br />

var getById = (document.getElementById) ? true : false;<br />

and so on. In some ways, object detection is the best method to use, but it should be used<br />

carefully and assumptions shouldn‘t be made.<br />

Another consideration with object detection is not to go too far too quickly. Remember that<br />

probing a property of a nonexistent object throws an error, so first check to see if the object<br />

exists. As an example, if you were checking for window.screen.height and you just did<br />

if (window.screen.height)<br />

// do something<br />

you would throw an error in browsers that did not support the Screen object. Instead you could<br />

rely on short-circuit evaluation to do the test incrementally, like so:<br />

if (window.screen && window.screen.height)<br />

// do something<br />

Advanced <strong>JavaScript</strong> programmers might see that the object detection approach fits nicely with<br />

try/catch blocks.<br />

Java Detection<br />

Detecting Java‘s availability is fairly easy using the Navigator method javaEnabled(). This<br />

method returns true if Java is available and turned on, and false otherwise.<br />

if (navigator.javaEnabled())<br />

else<br />

// do Java stuff or write out tag<br />

alert("Sorry no Java");<br />

You can find out more about Java once you know it is available by accessing a Java applet<br />

included in the page. You can even determine what type of Java Virtual Machine is supported.<br />

In order to do this, you will have to access the public methods and properties of a Java applet.<br />

Interacting with applets is discussed in more detail in Chapter 18.<br />

Plug-in Detection<br />

In Netscape 3+ (and Opera 4+), each plug-in installed in the browser has an entry in the<br />

plugins[] array of the Navigator object. Each entry in this array is a Plugin object containing<br />

information about the specific vendor and version of the component. A simple detection scheme<br />

checks for a plug-in‘s existence using the associative array aspect of <strong>JavaScript</strong> collections. For<br />

example, to look for a Flash plug-in, you might write<br />

if (navigator.plugins["Shockwave Flash"])

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!