Checking for Plug-Ins


if (new ActiveXObject(   "ShockwaveFlash.ShockwaveFlash.8")) { } 

In Mozilla browsers, the about:plugins special URL provides a list of all available plug-ins, as Figure 12.1 shows; this works in Opera as well.

Figure 12.1. Opera shows a list of all installed plugins.


Depending on the chosen browser, there are two ways to actively check for the presence of a certain plug-in:

  • On Mozilla browsers and Opera (which uses the same API), navigator.plugins is an array with all plug-ins. Every array element exposes the name, filename, and description of every plug-in. Also, navigator.mimeTypes contains a list of all supported MIME types the plug-in can support.

  • On Internet Explorer (Windows only), you can try to instantiate the associated class the plug-in exposes, by using the ActiveXObject constructor.

The following code tries to detect the Flash plug-in. For Internet Explorer, the name of the associated COM object is used; for other browsers, the code is looking for a plug-in called "Shockwave Flash."

Detecting the Flash Plug-In (plugin.html)

<script language="JavaScript" type="text/javascript"> var hasFlash = false; if (window.ActiveXObject) {   try {     if (new ActiveXObject(       "ShockwaveFlash.ShockwaveFlash.8")) {       hasFlash = true;     }   } catch (e) {   } } else if (navigator.plugins) {   if (navigator.plugins["Shockwave Flash"]) {       hasFlash = true;   } } window.alert(hasFlash); </script> 

Warning

The array navigator.plugins is also available on Internet Explorer, but empty. Therefore, you have to first check for window.ActiveXObject, then for navigator.plugins. It will not work the other way around.





JavaScript Phrasebook(c) Essential Code and Commands
JavaScript Phrasebook
ISBN: 0672328801
EAN: 2147483647
Year: 2006
Pages: 178

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net