JavaScript 调用 ActionScript 的简单方法
本文介绍在Flex环境中JavaScript调用ActionScript的实现方法。核心步骤包括:使用ExternalInterface.addCallback注册回调函数,通过Flash在网页中的ID获取实例,最后调用AS函数。相比AS调用JS,JS调用AS需额外处理实例获取环节。
ActionScript调用JavaScript的基础逻辑
在Flex开发中,由ActionScript调用JavaScript相对直接。其逻辑与HTML页面中调用JavaScript的方式类似:在ActionScript中如何发起调用,就按照相同的模式进行即可。
JavaScript调用ActionScript的实现步骤
反之,若需由JavaScript调用ActionScript,步骤稍显复杂,但整体流程依然清晰简便。
具体实现涉及MXML代码的配置。
<?xml version="1.0" encoding="utf-8"?>
<mx:Application xmlns:mx="http://www.adobe.com//mxml" layout="vertical" horizontalAlign="left" backgroundColor="white"
initialize="init()">
<mx:Label text="城市名称:"/>
<mx:List id="cityList" width="" height="" dataProvider="{cities}"/>
<mx:ArrayCollection id="cities">
<mx:String>北京</mx:String>
<mx:String>上海</mx:String>
</mx:ArrayCollection>
<mx:Script>
<![CDATA[
private function init(): void
{
//注册回调函数供JavaScript调用
ExternalInterface.addCallback("callActionScript", asFunctionByJs);
}
private function asFunctionByJs(city: String): void
{
cities.addItem(city);
}
]]>
</mx:Script>
</mx:Application>
同时需要配置HTML嵌入代码。这些HTML代码通常由Flex Builder自动生成,用于将Flash对象嵌入网页。开发者无需深入关注所有细节,只需留意黄色高亮部分,这是手动添加的关键配置。
<!-- saved from url=(0014)about:internet -->
<html lang="en">
<!--
Smart developers always View Source.
This application was built using Adobe Flex, an open source framework
for building rich Internet applications that get delivered via the
Flash Player or to desktops via Adobe AIR.
Learn more about Flex at http://flex.org
// -->
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-" />
<!-- BEGIN Browser History required section -->
<link rel="stylesheet" type="text/css" href="history/history.css" />
<!-- END Browser History required section -->
<title></title>
<script src="AC_OETags.js" language="javascript"></script>
<!-- BEGIN Browser History required section -->
<script src="history/history.js" language="javascript"></script>
<!-- END Browser History required section -->
<style>
body {}{ margin: px; overflow:hidden }
</style>
<script language="JavaScript" type="text/javascript">
<!--
// -----------------------------------------------------------------------------
// Globals
// Major version of Flash required
var requiredMajorVersion = 9;
// Minor version of Flash required
var requiredMinorVersion = 0;
// Minor version of Flash required
var requiredRevision = 124;
// -----------------------------------------------------------------------------
// -->
</script>
<script type="text/javascript">
function callActionScript(value)
{
//根据id获取flash实例,在这里id是CallAsFromJs,可以从Embed
var flash = (navigator.appName.indexOf ("Microsoft") !=-)?window["CallAsFromJs"]:document["CallAsFromJs"];
//调用ActionScript注册的回调方法
flash.callActionScript(value);
}
</script>
</head>
<body scroll="no">
输入城市名称:<input type="text" id="newCityName"/><input type="button" value="添加城市" onclick="callActionScript(newCityName.value);"/>
<script language="JavaScript" type="text/javascript">
<!--
// Version check for the Flash Player that has the ability to start Player Product Install (6.0r65)
var hasProductInstall = DetectFlashVer(6, 0, 65);
// Version check based upon the values defined in globals
var hasRequestedVersion = DetectFlashVer(requiredMajorVersion, requiredMinorVersion, requiredRevision);
if ( hasProductInstall && !hasRequestedVersion ) {
// DO NOT MODIFY THE FOLLOWING FOUR LINES
// Location visited after installation is complete if installation is required
var MMPlayerType = (isIE == true) ? "ActiveX" : "PlugIn";
var MMredirectURL = window.location;
document.title = document.title.slice(0, 47) + " - Flash Player Installation";
var MMdoctitle = document.title;
AC_FL_RunContent(
"src", "playerProductInstall",
"FlashVars", "MMredirectURL="+MMredirectURL+'&MMplayerType='+MMPlayerType+'&MMdoctitle='+MMdoctitle+"",
"width", "100%",
"height", "100%",
"align", "middle",
"id", "CallAsFromJs",
"quality", "high",
"bgcolor", "#ffffff",
"name", "CallAsFromJs",
"allowScriptAccess","sameDomain",
"type", "application/x-shockwave-flash",
"pluginspage", "http://www.adobe.com/go/getflashplayer"
);
} else if (hasRequestedVersion) {
// if we've detected an acceptable version
// embed the Flash Content SWF when all tests are passed
AC_FL_RunContent(
"src", "CallAsFromJs",
"width", "%",
"height", "%",
"align", "middle",
"id", "CallAsFromJs",
"quality", "high",
"bgcolor", "#ffffff",
"name", "CallAsFromJs",
"allowScriptAccess","sameDomain",
"type", "application/x-shockwave-flash",
"pluginspage", "http://www.adobe.com/go/getflashplayer"
);
} else { // flash is too old or we can't detect the plugin
var alternateContent = 'Alternate HTML content should be placed here. '
+ 'This content requires the Adobe Flash Player. '
+ '<a href=http://www.adobe.com/go/getflash/>Get Flash</a>';
document.write(alternateContent); // insert non-flash content
}
// -->
</script>
<noscript>
<object classid="clsid:DCDBE-AED-cf-B-"
id="CallAsFromJs" width="%" height="%"
codebase="http://fpdownload.macromedia.com/get/flashplayer/current/swflash.cab">
<param name="movie" value="CallAsFromJs.swf" />
<param name="quality" value="high" />
<param name="bgcolor" value="#ffffff" />
<param name="allowScriptAccess" value="sameDomain" />
<embed src="CallAsFromJs.swf" quality="high" bgcolor="#ffffff"
width="%" height="%" name="CallAsFromJs" align="middle"
play="true"
loop="false"
quality="high"
allowScriptAccess="sameDomain"
type="application/x-shockwave-flash"
pluginspage="http://www.adobe.com/go/getflashplayer">
</embed>
</object>
</noscript>
</body>
</html>
综上所述,实现JavaScript调用ActionScript主要包含以下三个步骤:
首先,在ActionScript端使用ExternalInterface.addCallback方法注册可供外部调用的回调函数。
其次,在JavaScript函数中,通过Flash对象在网页DOM中的唯一ID来获取对应的Flash实例。
最后,利用上一步获取的Flash实例,直接调用之前注册的ActionScript函数。
以上即为JavaScript调用ActionScript的完整实现流程,希望能对读者有所帮助。如有任何疑问,欢迎留言交流,小编将及时回复。同时感谢大家对本网站的支持。
JS处理含Emoji或中文的字符串转数组乱码问题
2026-11-05 16:12:28
JS怎么使用展开运算符合并对象
2026-11-05 14:42:18
ES6+环境下判断JS对象为空的简洁写法
2026-11-05 12:12:23
怎么通过replacer函数自定义JS中JSON的格式化输出
2026-11-05 11:19:44
JS正则表达式基础:字面量与RegExp构造函数的区别及常用元字符
2026-11-05 11:16:58
