WebReference.com - Part 3 of Chapter 6 from Designing with JavaScript (3/4)

Designing with JavaScript, 2nd Edition

With this script, Navigator and IE/Mac users are taken care of, IE/Windows users have
Flash automatically downloaded as a control, and anybody else can decide what they want to do. The
script is shown in Example 6-11.

In Example 6-11, we actually
check for the presence of navigator.mimeTypes and verify that the length of the
mimeTypes array isn't zero:

if (navigator.mimeTypes && navigator.mimeTypes.length != 0)

This is because IE on Windows systems actually defines a mimeTypes object
but leaves it empty. Checking the length allows us to handle IE on Windows
appropriately.

The script starts by checking whether the browser supports the navigator.mimeTypes
array. The technique shown here is another form of browser version checking called object detection. Instead
of checking the specific browser name and version, object detection checks for the presence of a particular
object, in this case, the mimeTypes object. This is much easier than checking to see if the
browser is Navigator 3 or later or IE 5 or later for the Mac, and it has the same results. If the
mimeTypes object is supported, the browser variable is set to "mimeTypes" to indicate this;
otherwise, the variable is set to "noMimeTypes".

If the browser supports mimeTypes, the script determines if the Flash plug-in
is available, by checking for the application/x-shockwave-flash MIME type. If that MIME type
is detected, we display the Flash movie using the embed tag. If the MIME type is not detected,
we display links to download the Flash plug-in and to enter the non-Flash site.

If the browser is IE (on Windows), we display the Flash ActiveX control using the
object tag. If this control is not available on the user's system, we give the user the
option to download it, as shown in Figure 6-6.

Finally, if there's no way for JavaScript to detect the presence of Flash, we display
a page with all possible optionsÂenter the Flash site, enter the non-Flash site, or download the
Flash plug-inÂso users can decide on their own what to do.

Different versions of Flash?

What if you need to check for a particular version of the Flash plug-in? You might,
for example, need to look for Flash Player 5, because your Flash movie makes extensive use of
ActionScript. Rather than checking for application/x-shockwave-flash in the
mimeTypes array, you can search the navigator.plugins array instead. The
first step is to give "Shockwave Flash" to the plugins array to see if any Flash plug-in is available:

var plugin = navigator.plugins["Shockwave Flash"];

If a Flash plug-in is available, this statement returns an object that has a description
property that contains the version number for the plug-in. Here's how we can get at that
information: