04.11.2015 Views

javascript

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

Chapter 8: The Browser Object Model<br />

In this example, the hasIEPlugin() function accepts a COM identifier as its sole argument. In the<br />

function, an attempt is made to create a new ActiveXObject instance. This is encapsulated in a try -<br />

catch statement because an attempt to create an unknown COM object will throw an error. Therefore, if<br />

the attempt is successful, the function returns true . If there is an error, the catch block gets executed,<br />

which returns false . This code then checks to see if the Flash and QuickTime plug - ins are available in IE.<br />

Since these two plug - in – detection methods are so different, it ’ s typical to create functions that test for<br />

specific plug - ins rather than using the generic methods described previously. Consider this example:<br />

//detect flash for all browsers<br />

function hasFlash(){<br />

var result = hasPlugin(“Flash”);<br />

if (!result){<br />

result = hasIEPlugin(“ShockwaveFlash.ShockwaveFlash”);<br />

}<br />

return result;<br />

}<br />

//detect quicktime for all browsers<br />

function hasQuickTime(){<br />

var result = hasPlugin(“QuickTime”);<br />

if (!result){<br />

result = hasIEPlugin(“QuickTime.QuickTime”);<br />

}<br />

return result;<br />

}<br />

//detect flash<br />

alert(hasFlash());<br />

//detect quicktime<br />

alert(hasQuickTime());<br />

This code defines two functions: hasFlash() and hasQuickTime() . Each function attempts to use the<br />

non - IE plug - in– detection code first. If that method returns false (which it will for IE), the IE plug - in<br />

detection method is called. If the IE plug - in–detection method also returns false , then the result of the<br />

overall method is false. If either plug - in–detection function returns true , then the overall method<br />

returns true .<br />

The plugins collection has a method called refresh() , which refreshes plugins to reflect any<br />

newly installed plug - ins. This method accepts a single argument: a Boolean value indicating if the page<br />

should be reloaded. When set to true , all pages containing plug - ins are reloaded; otherwise the<br />

plugins collection is updated but the page is not reloaded.<br />

Registering Handlers<br />

Firefox 2.0 introduced the registerContentHandler() and registerProtocolHandler() methods<br />

to the navigator object (these are defined in HTML 5, which is discussed in Chapter 22 ). These methods<br />

allow a web site to indicate that it can handle specific types of information. With the rise of online RSS<br />

readers and online e-mail applications, this is a way for those applications to be used by default just as<br />

desktop applications are used.<br />

223

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

Saved successfully!

Ooh no, something went wrong!