JavaScript and Embedded Objects - Interacting with ActiveX Controls (Page 14 of 15 )
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=iso-8859-1" />
<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.
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: Summary >>
More JavaScript Articles
More By McGraw-Hill/Osborne