第二部分 Javascript => Flash由页面上Flash外部的Javascript代码控制Flash内部的变量和对象属性。html:<HTML> <HEAD> <TITLE>javascript_to_flash</TITLE> </HEAD> <SCRIPT LANGUAGE=JavaScript> <!-- var InternetExplorer = navigator.appName.indexOf("Microsoft") != -1; function doPassVar(args){ var oFlash = InternetExplorer ? window.myFlash : window.document.myFlash; var sendText = args.value; oFlash.SetVariable("myVar", sendText); } //--> </SCRIPT> <BODY bgcolor="#FFFFFF"> <p><b><font face="Geneva, Arial, Helvetica">Example of JavaScript-to-Flash communication</font></b> </p> <p><font face="Geneva, Arial, Helvetica">Enter some text in the HTML form field below and then click outside the field, or hit the Tab key, to send the text to the Flash movie below:</font></p> <form name="form1" onSubmit="doPassVar(sendText);return false;" action="#"> <input type="text" name="sendText" maxlength="45" onChange="doPassVar(this);"> </form> <p><object classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000" codebase="http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=5,0,0,0" width=400 height=180 id="myFlash"> <param name=movie value="javascript_to_flash.swf"> <param name=quality value=high> <embed src="javascript_to_flash.swf" quality=high width=400 height=180 type="application/x-shockwave-flash" pluginspage="http://www.macromedia.com/shockwave/download/index.cgi?P1_Prod_Version=ShockwaveFlash" name="myFlash" swLiveConnect="true"> </embed> </object></p> </BODY> </HTML>javascript_to_flash.swf内部有一个动态输入框(变量myVar)
<HTML>
<HEAD>
<meta http-equiv=Content-Type content="text/html; charset=ISO-8859-1">
<TITLE>flash_to_javascript</TITLE>
</HEAD>
<BODY><SCRIPT LANGUAGE=JavaScript>
<!--
function myFlash_DoFSCommand(command, args)
{
alert (args);
}
if (navigator.appName && navigator.appName.indexOf("Microsoft") != -1 &&
navigator.userAgent.indexOf("Windows") != -1) {
document.write('<SCRIPT LANGUAGE=VBScript\> \n');
document.write('on error resume next \n');
document.write('Sub myFlash_FSCommand(ByVal command, ByVal args)\n');
document.write(' call myFlash_DoFSCommand(command, args)\n');
document.write('end sub\n');
document.write('</SCRIPT\> \n');
}
//-->
</SCRIPT><OBJECT classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000"
codebase="http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=5,0,0,0"
WIDTH="400" HEIGHT="180" id="myFlash">
<PARAM NAME=movie VALUE="flash_to_javascript.swf"> <PARAM NAME=quality VALUE=high> <PARAM NAME=bgcolor VALUE=#CCCCCC>
<EMBED src="flash_to_javascript.swf" quality=high bgcolor=#CCCCCC WIDTH="400" HEIGHT="180" NAME="myFlash" TYPE="application/x-shockwave-flash" PLUGINSPAGE="http://www.macromedia.com/go/getflashplayer"></EMBED>
</OBJECT>
</BODY>
</HTML>flash_to_javascript.swf:
内部有一个输入文本框(变量inputVar),和一个触发按钮,按钮事件为:on (release) {
fscommand ("send_var", inputVar);
}flash与javascript通信有两种机制:IE的ActiveX和Netscape(Mozilla)的Plugin机制前者通过VBScript函数来捕捉Flash内部抛出的fscommand命令
注意函数名格式是固定的: ObjectID_FSCommandsub myFlash_FSCommand(ByVal command, ByVal args)
call myFlash_DoFSCommand(command, args)
end sub后者通过Javascript函数实现捕捉.
注意函数名格式是固定的: EmbedName_DoFSCommand
function myFlash_DoFSCommand(command, args)
{
alert (args);
}备注:
不知道为何源于Mozilla的Firefox不能使用Mozilla的通信机制实现交互。同样的Opera也无效。
<HEAD>
<TITLE>javascript_to_flash</TITLE>
</HEAD>
<SCRIPT LANGUAGE=JavaScript>
<!--
var InternetExplorer = navigator.appName.indexOf("Microsoft") != -1;
function doPassVar(args){
var oFlash = InternetExplorer ? window.myFlash : window.document.myFlash;
var sendText = args.value;
oFlash.SetVariable("myVar", sendText);
}
//-->
</SCRIPT>
<BODY bgcolor="#FFFFFF">
<p><b><font face="Geneva, Arial, Helvetica">Example of JavaScript-to-Flash communication</font></b>
</p>
<p><font face="Geneva, Arial, Helvetica">Enter some text in the HTML form field
below and then click outside the field, or hit the Tab key, to send the text
to the Flash movie below:</font></p>
<form name="form1" onSubmit="doPassVar(sendText);return false;" action="#">
<input type="text" name="sendText" maxlength="45" onChange="doPassVar(this);">
</form>
<p><object classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000"
codebase="http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=5,0,0,0"
width=400 height=180 id="myFlash">
<param name=movie value="javascript_to_flash.swf">
<param name=quality value=high>
<embed src="javascript_to_flash.swf" quality=high width=400 height=180 type="application/x-shockwave-flash" pluginspage="http://www.macromedia.com/shockwave/download/index.cgi?P1_Prod_Version=ShockwaveFlash" name="myFlash" swLiveConnect="true">
</embed>
</object></p>
</BODY>
</HTML>javascript_to_flash.swf内部有一个动态输入框(变量myVar)
seeu1688(kangel) 提到的“不知道为何源于Mozilla的Firefox不能使用Mozilla的通信机制实现交互。同样的Opera也无效。” 我想是不是基于安全考虑?!
javascript->flash
只能用
oFlash.SetVariable("myVar", sendText);吗