JavaScript and Embedded Objects - Detecting Specific Plug-Ins (Page 8 of 15 )
In Netscape 3+, Opera 4+, and Mozilla-based browsers, each plug-in installed in the browser has an entry in the plugins[] array of the Navigator object. Each entry in this array is a Plugin object containing information about the specific vendor and version of the component installed. Some interesting properties of the Plugin object are listed in Table 18-2.
Each Plugin object is an array of the MimeType objects that it supports (hence its length property). You can visualize the plugins[] and mimeTypes[] arrays as being cross-connected. Each element in plugins[] is an array containing references to one or more elements in mimeTypes[]. Each element in mimeTypes[] is an object referred to by exactly one element in plugins[], the element referred to by the MimeType’s pluginEnabled reference.
You can refer to the individual MimeType objects in a Plugin element by using double-array notation:
navigator.plugins[0][2]
This example references the third MimeType object supported by the first plug-in.
More useful is to index the plug-ins by name. For example, to write all the MIME types supported by the Flash plug-in (if it exists!), you might write
if (navigator.plugins["Shockwave Flash"])
{
for (var i=0; i<navigator.plugins["Shockwave
Flash"].length; i++)
document.write("Flash MimeType: " + navigator.plugins
["Shockwave Flash"][i].type + "<br />");
}
Of course, as with all things plug-in–related, you need to read vendor documentation very carefully in order to determine the exact name of the particular plug-in in which you are interested.
| Property | Description |
| description | String describing the nature of the plug-in. Exercise caution with this property because this string can be rather long. |
| name | String indicating the name of the plug-in. |
| length | Number indicating the number of MIME types this plug-in is currently supporting. |
TABLE 18-2 Some Interesting Properties of the Plugin Object
To illustrate the composition of the Plugin object more clearly, the following code prints out the contents of the entire plugins[] array:
for (var i=0; i<navigator.plugins.length; i++)
{
document.write("Name: " + navigator.plugins[i].name + "<br />");
document.write("Description: " + navigator.plugins[i].description + "<br />");
document.write("Supports: ");
for (var j=0; j<navigator.plugins[i].length; j++)
document.write(" " + navigator.plugins[i][j].type);
// the nonbreaking space included so the types are more readable
document.write("<br /><br />");
}
The results are shown in Figure 18-5.
Dealing with Internet Explorer
One thing to be particularly conscious of is that Internet Explorer defines a faux plugins[] array as a property of Navigator. It does so in order to prevent poorly written Netscapespecific scripts from throwing errors while they probe for plug-ins. Under Internet Explorer, you have some reference to plug-in–related data through the document.embeds[] collection. However, probing for MIME types and other functions is not supported, since Explorer actually uses ActiveX controls to achieve the function of plug-ins included via an <embed> tag. For more information on using JavaScript with ActiveX, see the section entitled “ActiveX” later in this chapter. For now, simply consider that to rely solely on information from navigator.plugins[] without first doing some browser detection can have some odd or even disastrous consequences.

FIGURE 18-5 Example contents of the navigator.plugins[] array
This chapter is from JavaScript: The Complete Reference, second edition, by Thomas Powell and Fritz Schneider, McGraw-Hill/Osborne, ISBN: 0072253576). Check it out at your favorite bookstore today.
Buy this book now. |
Next: Interacting with Plug-Ins >>
More JavaScript Articles
More By McGraw-Hill/Osborne