JavaScript Editor JavaScript Debugger     JavaScript Editor 



Team LiB
Previous Section Next Section

ActiveX

ActiveX is a Microsoft component object technology enabling Windows programs to load and use other programs or objects at runtime. ActiveX controls are basically subprograms launched by the browser that can interact with page content. For example, if a <<textarea>> provided insufficient editing capabilities for a particular task, the page author might include an ActiveX control that provides an editor interface similar to that of MS Word.

While on the surface ActiveX controls might seem a lot like Java applets, the two technologies are not at all alike. For one, once an ActiveX control is installed on the user’s machine, it is given greater access to the local system. This loosened security stance means that controls can access and change files, and do all manner of other powerful yet potentially unsavory things. Since ActiveX controls are executable code, they are built for a specific operating system and platform. This means that they are minimally supported outside of Internet Explorer, and not at all outside of Windows.

Whereas Java applets are downloaded when they are needed, ActiveX controls are, like plug-ins, persistent once they are installed. This installation process is often automatic, which is both good and bad. It is good in the sense that it obviates the need to have the user manually install a required component. But it is also a security risk because most users could be easily fooled into accepting the installation of a malicious control. We’ll have more to say about the security of ActiveX controls in Chapter 22.

Including ActiveX Controls

An ActiveX control is embedded in the page using an <<object>> tag with the classid attribute specifying the GUID (Globally Unique Identifier) of the ActiveX control you wish to instantiate. The syntax is similar to that of the <<object>> syntax for the inclusion of applets. Parameters are passed using <<param>> elements, and anything included between the <<object>>‘s opening and closing tags is processed by non-<<object>>-aware browsers; for example:

<<object classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000"
codebase="http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#
version=6,0,40,0" name="demoMovie" id="demoMovie" width="318" height="252">>
<<param name="movie"
 value="http://www.javascriptref.com/examples/ch18/flash.swf" />>
<<param name="play" value="true" />>
<<param name="loop" value="false" />>
<<param name="quality" value="high" />>

<<em>>Your browser does not support ActiveX!<</em>>

<</object>>

This example defines an embedded Flash file for use with an ActiveX control. In general, ActiveX controls have classid attributes beginning with “clsid:.” We saw another possibility in a previous section where the classid began with “java:.” In general, the classid attribute specifies the unique identifier of the control for which the data is intended. The classid value for each ActiveX control is published by the vendor, but it is also commonly inserted by Web development tools such as Macromedia Dreamweaver (www.macromedia.com/dreamweaver).

The final item of note is the codebase attribute specifying the version of the ActiveX binary that is required for this particular object. The classid and codebase attributes serve the function that manual probing of plug-ins does under Netscape. If the user’s machine doesn’t have the required control or version, the user will be prompted to download it from the given location.

Cross-Browser Inclusion of Embedded Objects

By far the best way to ensure the cross-browser compatibility of your pages is to use a combination of ActiveX controls and plug-in syntax. To accomplish this, use an <<object>> intended for IE/Windows ActiveX controls and include within it an <<embed>> intended for Netscape and IE/Macintosh plug-ins. The technique is illustrated in the following example:

<<object classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000"
 codebase="http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#\
version=6,0,40,0"
 name="demoMovie" id="demoMovie" width="318" height="252">>
<<param name="movie" value=http://www.javascriptref.com/examples/ch18/flash.swf
 />>
<<param name="play" value="true" />>
<<param name="loop" value="false" />>
<<param name="quality" value="high" />>

<<embed src="http://www.javascriptref.com/examples/ch18/flash.swf"
 width"318" height="252" play="true" loop="false" quality="high"
pluginspage="http://www.macromedia.com/go/getflashplayer">>
<<noembed>>
  Error: No Object or Embed Support
<</noembed>>
<</embed>>

<</object>>

Browsers that do not understand <<object>> will see the <<embed>>, whereas browsers capable of processing <<object>> will ignore the enclosed <<embed>>. Using <<object>> and <<embed>> in concert maximizes the possibility that the user will be able to process your content.

Interacting with ActiveX Controls

JavaScript can be used to interact with ActiveX controls in a manner quite similar to plug-ins. A control is accessible under the Document object according to the id of the <<object>> that included it. If the required control isn’t available, Internet Explorer automatically installs it (subject to user confirmation) and then makes it available for use.

Note 

You may have to include the mayscript attribute in the <<object>> to enable callback functions.

Any methods exposed by the control are callable from JavaScript in the way applet or plug-in functionality is called. Simply invoke the appropriate function of the <<object>> in question. To invoke the Play() method of the control in the previous example, you’d write

document.demoMovie.Play();

As a quick demonstration, we recast the previous example so it works in both Netscape and Internet Explorer browsers.

<<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" 
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">>
<<html xmlns="http://www.w3.org/1999/xhtml">>
<<head>>
<<title>>Cross-browser Flash Control Example <</title>>
<<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />>
<<script type="text/javascript">>
<<!--
  var dataReady = false;
  var pluginAvailable = false;
  function detectPlugin()
   {
    if (navigator.plugins &&
        ((navigator.plugins["Shockwave Flash"] &&
          navigator.plugins["Shockwave Flash"]["application/x-shockwave-flash"])
         ||
         (navigator.plugins["Shockwave Flash 2.0"] &&
          navigator.plugins["Shockwave Flash 2.0"]["application/x-shockwave-flash"])
        ))
      pluginAvailable = true;
      return(pluginAvailable);
}

function changeFrame(i)
{
     if (!dataReady)
        return;
     // Some versions of the ActiveX control don't support TotalFrames,
     // so the check is omitted here. However, the control handles values
     // out of range gracefully.
     document.demo.GotoFrame(parseInt(i));
}

function play()
{
     if (!dataReady)
        return;
    if (!document.demo.IsPlaying())
      document.demo.Play();
}

function stop() 
{
     if (!dataReady)
        return;
     if (document.demo.IsPlaying())
        document.demo.StopPlay();
}

function rewind() 
{
     if (!dataReady)
        return;
     if (document.demo.IsPlaying())
        document.demo.StopPlay();
     document.demo.Rewind();
}
function zoom(percent) 
{
     if (!dataReady)
        return;
     if (percent >> 0)
        document.demo.Zoom(parseInt(percent));
}
//-->>
<</script>>
<</head>>
<<body onload="dataReady = true;">>
<<object id="demo" classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000" 
width="318" 
height="300" 
codebase="http://active.macromedia.com/flash2/cabs/swflash.cab#version=5,0,0,0">>
<<param name="movie" value="http://demos.javascriptref.com/jscript.swf" />>
<<param name="play" value="false" />>
<<param name="loop" value="false" />>
<<script type="text/javascript">>
<<!--
   if (detectPlugin()) 
    {
      document.write('<<embed name="demo" src="http://demos.javascriptref.com/jscript.swf" width="318" height="300" 
play="false" loop="false" pluginspage="http://www.macromedia.com/shockwave/download/index.cgi?P1_Prod_
Version=ShockwaveFlash5" swliveconnect="true">><</embed>>');
   } 
  else
    {
      // you can write an image in here in a "real" version     
      document.write('Macromedia Flash is required for this demo');
    }
//-->>
<</script>>
<<noscript>>
  JavaScript is required to demonstrate this functionality!
<</noscript>>
<</object>>
<<form name="controlForm" id="controlForm" onsubmit="return false;" action="#" 
method="get">>
<<input type="button" value="Start" onclick="play();" />>
<<input type="button" value="Stop" onclick="stop();" />>
<<input type="button" value="Rewind" onclick="rewind();" />><<br />>
<<input type="text" name="whichFrame" id="whichFrame" />>
<<input type="button" value="Change Frame"
 onclick="changeFrame(controlForm.whichFrame.value);" />><<br />>
<<input type="text" name="zoomValue" id="zoomValue" />>
<<input type="button" value="Change Zoom" 
onclick="zoom(controlForm.zoomValue.value)" />> (greater than 100 to zoom out, less
 than 100 to zoom in)<<br />>
<</form>>
<</body>>
<</html>>

You might wonder if ActiveX controls can do everything plug-ins can. The answer: yes, and even more. For example, data handled by ActiveX controls can take full advantage of callback functions, so everything that is possible with a plug-in is possible with ActiveX. Further, because data destined for ActiveX is embedded in <<object>> elements, it can take full advantage of the <<object>> event handlers defined in (X)HTML. Interestingly, there seems to be more robust support for ActiveX in VBScript than in JavaScript. This is most likely a result of the fact that as a Microsoft technology, VBScript is more closely coupled with Microsoft’s COM. For more information on ActiveX, see http://www.microsoft.com/com/tech/activex.asp.


Team LiB
Previous Section Next Section


JavaScript Editor JavaScript Debugger     JavaScript Editor


©