Detecting Plugins - Java Script

As with most Web technology, two types of plugins are available:those from Microsoft and those from others.The Microsoft way uses ActiveX technology,which you may remember as the way to create XML-related objects; the other way has been called Netscape-style plugins because Netscape Navigator introduced the concept of plugins to the Web.Until recently,Internet Explorer supported Netscape-style plugins in addition to ActiveX controls.Beginning with Internet Explorer 5.5 Service Pack 2(Windows only), however,Microsoft eliminated support for the Netscape-style plugins.

The main difference between the two types of plugins is the architecture.ActiveX plugins are built on Microsoft’s ActiveX platform whereas Netscape-style plugins are built on top of the Netscape Plugin API. Originally, every browser(including Internet Explorer) was forced to support the Netscape-style plugins because Netscape was the dominant browser and only compatible browsers could compete.

Although many will speculate why Microsoft ended support for Netscape-style plugins,it has created a clear rift in the world of plugins.Many plugin developers are required to create both Netscape-style plugins and ActiveX wrappers for these plugins in order to support Internet Explorer.

Today, browsers are separated into those that don’t support Netscape-style plugins(such as Internet Explorer on Windows) and those that do(such as Mozilla,Opera, Safari, and many other browsers).

Because of these differences, you have different ways of detecting whether a plugin is installed on a given browser.

Detecting Netscape-style plugins

Since Netscape 3.0, many browsers(notably those based on Mozilla) allow JavaScript to determine which MIME types are mapped to plugins,ultimately enabling you to determine if a given plugin is installed. This is determined through the help of the window.mimeTypes collection.

Two types of Netscape-style plugins exist:the style used in Netscape 4.x and the newer Gecko style used in Mozilla.The details of the differences are of no consequence to JavaScript developers because both styles are accessed the same way.

Each MIME type registered to a plugin is present in window.mimeTypes,indexed both by number and by MIME type,allowing you to access a MIME type directly or to iterate through the collection. An object with four properties represents each MIME type:

  • description— A description of the type of file represented by the MIME type
  • enabledPlugin— Reference to a plugin object with information about the specific plugin
  • suffixes—The file suffixes associated with this MIME type (such as mapping .gif to image/gif)
  • type— The MIME type

You can print out a list of all visible MIME types and their descriptions by running a simple script:

This example doesn’t print out any information about enabledPlugin, which is another object with another set of properties:

  • description— A description of the plugin
  • filename— The plugin filename
  • length— The number of MIME types associated with the plugin
  • name— The name of the plugin

It’s worth noting that not every MIME type has a plugin associated with it,meaning that enabledPlugin can be null.The example can be updated to include plugin information keeping this in mind:

This example outputs each MIME type complete with its plugin information(if one is available).

Just like the previous example,this page prints out the MIME types that are numerically indexed. Some MIME types are indexed only by the MIME type string, and these are not accessible using this approach. For instance, text/html is a registered MIME type with the browser, but it does not appear in the list generated from the previous code. However, you can explicitly test for it:

alert(navigator.mimeTypes[“text/html”] != null);

These invisible MIME types are typically those handled by the browser itself and usually don’t have a plugin registered to them.A simple page can be used to test any MIME type you can dream up:

The HTML page listed previously allows you to enter a MIME type into a text box and click a button to determine if a MIME type is registered.If so, you can tell whether it has a browser plugin. The findPlugin() function retrieves the value from the text box and first checks to see if the navigator .mimeTypes collection exists.If it does,then the function continues on to see if the MIME type is defined.If it is,you can tell whether it has a plugin installed.

You can extract the methodology to test for a plugin that handles a particular MIME type into a separate function:

Then, you can check for the existence of a given plugin like so:

It’s also possible to retrieve a list of all registered plugins without using the MIME types. The navigator.plugins collection contains all plugins, indexed by name and by number,available for the given browser. Each entry in the collection is a plugin object equivalent to the one referenced by enabledPlugin for each MIME type. So,if you know the name of a plugin (equivalent to its name property),you can access it directly:

var oFlashPlugin = navigator.plugins[“Shockwave Flash”];

Otherwise, you can iterate through the plugins to print out the information about each one. A simple page can also be used to do this:

This page prints out a list of all registered plugins and their descriptions.But the plugin object holds a secret;it is actually a collection of MIME types, meaning that MIME types for the plugin can be accessed like this:

var oMimeType = navigator.plugins[0][0];

This line of code accesses the first MIME type supported by the first plugin. So now, you can update the previous example to include the registered MIME types for each plugin:

Now this example displays each plugin’s name and description, followed by a list of supported MIME types.

The last thing to be aware of is that the navigator.plugins collection can sometimes become out of date, or stale, as the user is downloading a plugin required for viewing your page. To prepare for this possibility, you should always start out by refreshing navigator.plugins by calling the refresh() method. This method accepts one argument, a Boolean value, indicating whether the browser should reload the page (or pages) using an embedded object; to reload the pages, pass in true,otherwise, pass in false. For example: navigator.plugins.refresh(true); //reload all pages using plugins Making this simple call could save you some heartache later on.

Detecting ActiveX plugins

Because Internet Explorer plugins are just ActiveX controls, all you need to know is the name of the control to detect if it is installed.

Earlier in this book,you saw some code to detect the latest version of the Microsoft XML DOM. This same methodology can be used for any IE plugin. But how do you find the name of the ActiveX control you’re interested in?

Microsoft has a tool called the OLE/COM Object Viewer that can be used to find the ActiveX control name for all ActiveX controls installed on your computer.After it is installed, it provides a list of all OLE, COM, and ActiveX objects installed on your machine as well as important information about each one. It may take some time to look through all of the installed objects,but once you find the one you’re looking for, all the pertinent information is displayed .

The important piece of information is listed as VersionIndependentProgID, which gives you the name of the generic ActiveX control that creates the most recent version of the control. The ProgID listed typically gives you a version-specific control name, which helps to determine if a specific version is installed(although it doesn’t always list the highest version available). The following table lists the versionindependent and version-specific ActiveX control names for several popular plugins.

Detecting ActiveX plugins

Detecting ActiveX plugins

This function’s purpose is to determine if the Flash Player is installed. To do so, it tries to create the ActiveXObject with the name ShockwaveFlash.ShockwaveFlash. If this is successful, the next line executes and the function returns true. If the object can’t be created, the error is caught and the function returns false. This same basic algorithm can be used for any plugin with an ActiveX wrapper.

Cross-browser detection

Unfortunately, you have no easy way to establish cross-browser plugin detection in a generic way. In order to accurately detect if a plugin is available, you must know both the MIME type and the ActiveX control name. Most of the time, developers just create specific detection functions that include both pieces of information, such as the following:

This function uses object detection to determine which methodology to use. If navigator.mimeTypes is available,then the function checks for an installed plugin using Netscape-style plugin detection. If, on the other hand, it’s possible to create ActiveXObject objects, it uses the IE-style of detection. If neither of these options is available (which isn’t very likely), the function just returns false.To detect specific plugins, you can customize this algorithm and create a whole host of plugin detection functions.


All rights reserved © 2018 Wisdom IT Services India Pvt. Ltd DMCA.com Protection Status

Java Script Topics