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 />

name = name.toLowerCase();<br />

for (var i=0; i < navigator.plugins.length; i++){<br />

if (navigator.plugins[i].name.toLowerCase().indexOf(name) > -1){<br />

return true;<br />

}<br />

}<br />

}<br />

return false;<br />

//detect flash<br />

alert(hasPlugin(“Flash”));<br />

//detect quicktime<br />

alert(hasPlugin(“QuickTime”));<br />

The hasPlugin() example accepts a single argument: the name of a plug - in to detect. The first step is to<br />

convert that name to lowercase for easier comparison. Next, the plugins array is iterated over and each<br />

name property is checked via indexOf() to see if the passed - in name appears somewhere in that string.<br />

This comparison is done in all lowercase to avoid casing errors. The argument should be as specific as<br />

possible to avoid confusion. String such as “ Flash ” and “ QuickTime ” are unique enough that there<br />

should be little confusion. This method works for detecting plug - ins in Firefox, Safari, Opera, and Chrome.<br />

Each plugin object is also an array of MimeType objects that can be accessed using<br />

bracket notation. Each MimeType object has four properties: description , which is<br />

a description of the MIME type; enabledPlugin , which is a pointer back to the<br />

plugin object; suffixes , which is a comma - delimited string of file extensions for<br />

the MIME type; and type , which is the full MIME type string.<br />

Detecting plug - ins in IE is more problematic, because it doesn ’ t support Netscape - style plug - ins.<br />

The only way to detect plug - ins in IE is to use the proprietary ActiveXObject type and attempt to<br />

instantiate a particular plug - in. Plug - ins are implemented in IE using COM objects, which are identified<br />

by unique strings. So to check for a particular plug - in, you must know its COM identifier. For instance,<br />

the identifier for Flash is ” ShockwaveFlash.ShockwaveFlash ” . With this information, you can write a<br />

function to determine if the plug - in is installed in IE as follows:<br />

//plugin detection for IE<br />

function hasIEPlugin(name){<br />

try {<br />

new ActiveXObject(name);<br />

return true;<br />

} catch (ex){<br />

return false;<br />

}<br />

}<br />

//detect flash<br />

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

//detect quicktime<br />

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

222

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

Saved successfully!

Ooh no, something went wrong!