Embedding Sound in Web Pages, Part I: Plug-ins and Plugin objects - Doc JavaScript | WebReference

Embedding Sound in Web Pages, Part I: Plug-ins and Plugin objects - Doc JavaScript


Plug-ins and Plugin objects

A plug-in is a software module which is installed on the user's machine for the purpose of displaying specialized types of data embedded in Web pages. If you're running Navigator, you can discover which plug-ins are installed by selecting the "About Plug-ins" option from the "Help" menu. There are hundreds of plug-ins available on the Web, for displaying audio, video, 3D/VRML, and more. An audio plug-in must be installed in order to play an audio file, unless the browser is equipped with built-in audio support. When you embed sound in your Web pages, it is important to check that the required plug-in is indeed installed on the user's machine. You cannot take it for granted, as some users install their browsers without plug-ins. If you don't take the right measures to avoid displaying audio when the necessary plug-in is missing, the browser will display an alert.

Some versions of Netscape Navigator 3.0x and 4.0x come with the LiveAudio plug-in, while some versions of Microsoft Internet Explorer 4.0x include the ActiveMovie Control, which is required in order to play sounds.

In Navigator, an installed plug-in is represented by a JavaScript Plugin object. A Plugin object stores all available information about the relevant plug-in and MIME types supported by that plug-in. The following table summarizes the properties of a Plugin object:

PropertyDescription
nameThe name of the plug-in.
descriptionA short description of the plug-in.
filenameThe name of plug-in's file on disk.
lengthThe length of the plug-in's array of MimeType objects. In other words, this property reflects the number of MIME types supported by the plug-in.

All Plugin objects can be accessed through the navigator.plugins array. An individual Plugin object can be accessed through the plugins array. For example, plugins[0] is a Plugin object, representing one of the browser's plug-ins. Remember that the navigator.plugins array (and the Plugin object) is only supported by Navigator. In Internet Explorer, the navigator.plugins property is an empty array. Plugin objects can also be referenced by their name. The LiveAudio plugin object, for example, is plugins["LiveAudio"]. The properties of LiveAudio plugin can be found by printing the following variables:

<SCRIPT LANGUAGE="JavaScript1.1">
<!--
if (navigator.plugins["LiveAudio"]) {
  var liveAudioName = navigator.plugins["LiveAudio"].name;
  var liveAudioDescription = navigator.plugins["LiveAudio"].description;
  var liveAudioFilename = navigator.plugins["LiveAudio"].filename;
  var liveAudioMimeCount = navigator.plugins["LiveAudio"].length;
  document.write("name = ", liveAudioName, "<BR>",
    "description = ", liveAudioDescription, "<BR>",
    "filename = ", liveAudioFilename, "<BR>",
    "length = ", liveAudioMimeCount);
}
// -->
</SCRIPT>

For example, the script's output could be:

name = LiveAudio
description = Sound Player for Netscape Navigator, v.1.1.1513
filename = D:\PROGRAM FILES\NETSCAPE\COMMUNICATOR -->
4.04\PROGRAM\plugins\npaudio.dll
length = 9

The script looks very simple, but there are several important issues. First of all, notice that the LANGUAGE attribute is set to "JavaScript1.1". That is, the script is only executed if the browser is Internet Explorer 4.0x (and above) or Navigator 3.0x (and above). LiveAudio, Navigator's audio plug-in, is only installed in various versions of Navigator 3.0x and 4.0x. Furthermore, the navigator.plugins property is an empty collection in Internet Explorer 4.0x, so we can access its "properties," but none actually exist. The if statement, as explained in Column 6, Browser Compatibility, is used to check if an existing object features a specific property.

Let's take another look at the statements referring to the LiveAudio plug-in's Plugin properties:

var liveAudioName = navigator.plugins["LiveAudio"].name;
var liveAudioDescription = navigator.plugins["LiveAudio"].description;
var liveAudioFilename = navigator.plugins["LiveAudio"].filename;
var liveAudioMimeCount = navigator.plugins["LiveAudio"].length;

All four properties refer to the same parent object. Instead of specifying the object's entire path each time, we can assign its reference to a variable:

var liveAudio = navigator.plugins["LiveAudio"];
var liveAudioName = liveAudio.name;
var liveAudioDescription = liveAudio.description;
var liveAudioFilename = liveAudio.filename;
var liveAudioMimeCount = liveAudio.length;

Now suppose we want to find out what navigator.plugins[0] reflects. The following script segment shows how:

var liveAudioName = navigator.plugins[0].name;
var liveAudioDescription = navigator.plugins[0].description;
var liveAudioFilename = navigator.plugins[0].filename;
var liveAudioMimeCount = navigator.plugins[0].length;

Once again, you can assign navigator.plugins[0] to a variable. Note that each Plugin object also serves as an array containing one element for each MIME type supported by the plug-in module. The referencing schemes above apply to MIME types as well. You can reference them by name or by index. MIME types are covered extensively later in this column. The following code segment determines whether or not the LiveAudio plug-in supports "audio/wav" files:

if (navigator.plugins["LiveAudio"] && 
navigator.plugins["LiveAudio"]["audio/wav"])
  alert("supported")
else
  alert("not supported");

Notice that the script also checks if the LiveAudio plug-in exists, because accessing a property of an object that does not exist generates an error (as opposed to an unexisting property of an existing object). Once again, remember that navigator.plugins is an empty array in Internet Explorer, so the script would display "not supported."

http://www.internet.com


Created: May 31, 1998
Revised: May 31, 1998

URL: http://www.webreference.com/js/column20/pluginsobject.html