实现了ActionScript与JavaScript交互,通过函数来实现2者的数据交互
演示地址:http://scboy.ik8.com/JavaScriptExample.htm
AsJsCall.html文件代码:
<!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>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
<title>AsJsCall</title>
<style type="text/css">
ul{list-style:none; border:dotted; width:350px;};
li{padding:5px};
</style>
<script type="text/javascript">
/********* Start AS Call JS Function ***********************/
// 获得在Flash中输入的姓名和性别,并返回显示出来
function getNameSexFromAS( strName, strSex )
{
document.getElementById("txtNameFromAS").value = strName;
document.getElementById("txtSexFromAS").value = strSex;
}
/********* End AS Call JS Function ***********************/
/********* Start JS Call AS Function ***********************/
// 获得HTML页面中嵌入的Flash文件
function thisMovie( movieName )
{
// 判断是否为IE浏览器
var isIE = navigator.appName.indexOf("Microsoft") != -1;
return (isIE)?window[movieName]:document[movieName];
}
function callASFunction( strName, strSex )
{
// 调用AS中的getNameSexFromJS方法
thisMovie( "extint" ).getNameSexFromJS( strName, strSex );
// 用这句也可以
// document.getElementById("extint").getNameSexFromJS( strName, strSex );
}
/********* End JS Call AS Function ***********************/
</script>
</head>
<body>
<object classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000" codebase="http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=7,0,19,0"
id="extint"
width="300"
height="250"
title="AsJsCall"
>
<param name="movie" value="AsJsCall.swf" />
<param name="quality" value="high" />
<embed src="AsJsCall.swf" quality="high" pluginspage="http://www.macromedia.com/go/getflashplayer" type="application/x-shockwave-flash" width="300" height="250"></embed>
</object>
<ul>
<li>请输入你的姓名ToAS: <input type="text" id="txtName" /></li>
<li>请输入你的性别ToAS: <input type="text" id="txtSex" /></li>
<li><input type="button" id="btnGetFromAS" value="调用AS方法" onclick="javascript:callASFunction( document.getElementById('txtName').value, document.getElementById('txtSex').value )"/></li>
</ul>
<ul>
<li>显示AS中输入的姓名和性别:</li>
<li> <input type="text" id="txtNameFromAS" /></li>
<li> <input type="text" id="txtSexFromAS" /></li>
</ul>
</body>
</html>
AsJsCall.fla: Flash文件代码:
import flash.external.*;
/********* Start AS Call JS Function ***********************/
// 添加click监听事件
btnCallJSFun.addEventListener( "click", doClick );
// 用户单击btnCallJSFun引发click事件,并执行事件处理函数
function doClick( eventObj:Object ):Void{
//调用JS的getNameSexFromAS方法,后面txtNameToJS.text, txtSexToJS.text为传替的参数
ExternalInterface.call( "getNameSexFromAS", txtNameToJS.text, txtSexToJS.text );
};
/********* End AS Call JS Function ***********************/
/********* Start JS Call AS Function ***********************/
function getNameSexFromJS( strName:String, strSex:String ):Void
{
txtName.text = strName;
txtSex.text = strSex;
}
// 注册该getNameSexFromJS方法为JS可以调用的方法
// "getNameSexFromJS"为别名,可以随意写
ExternalInterface.addCallback( "getNameSexFromJS", this, getNameSexFromJS )
/********* End JS Call AS Function ***********************/
评论