Javascript 调用 ActionScript 的简单方法


Posted in Javascript onSeptember 22, 2016

1. 在Flex中,ActionScript调用Javascript是比较简单的,说白了就是,在html里,怎么调用Javascript,在ActionScript就怎么调用就可以了

2. 如果用js调用as,就稍微麻烦一点,其实也比较简单

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代码(这些代码都是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>

总结,js调用as,大概分为3步:

1.as使用ExternalInterface.addCallback注册回调函数

2.在js函数中根据flash在网页中的id获取实例

3.用上面获取到flash实例,调用as的函数

以上所述是小编给大家介绍的Javascript 调用 ActionScript 的简单方法,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对三水点靠木网站的支持!

Javascript 相关文章推荐
Highslide.js是一款基于js实现的网页中图片展示插件
Mar 30 Javascript
下载网站打开页面后间隔多少时间才显示下载链接地址的代码
Apr 25 Javascript
JQuery菜单效果的两个实例讲解(3)
Sep 17 Javascript
js实现非常棒的弹出div
Oct 06 Javascript
Bootstrap组件之下拉菜单,多级菜单及按钮布局方法实例
May 25 Javascript
Angular4实现动态添加删除表单输入框功能
Aug 11 Javascript
利用CDN加速react webpack打包后的文件详解
Feb 22 Javascript
vue3.0 CLI - 2.1 -  component 组件入门教程
Sep 14 Javascript
Webstorm2016使用技巧(SVN插件使用)
Oct 29 Javascript
JavaScript字符串处理常见操作方法小结
Nov 15 Javascript
浅析vue-router实现原理及两种模式
Feb 11 Javascript
WebStorm无法正确识别Vue3组合式API的解决方案
Feb 18 Vue.js
JavaScript与ActionScript3两者的同性与差异性
Sep 22 #Javascript
ionic由于使用了header和subheader导致被遮挡的问题的两种解决方法
Sep 22 #Javascript
自制微信公众号一键排版工具
Sep 22 #Javascript
IONIC自定义subheader的最佳解决方案
Sep 22 #Javascript
详解Node.js中的事件机制
Sep 22 #Javascript
AngularJS通过$sce输出html的方法
Sep 22 #Javascript
JavaScript 随机验证码的生成实例代码
Sep 22 #Javascript
You might like
php json_encode值中大括号与花括号区别
2013/09/30 PHP
php生成4位数字验证码的实现代码
2015/11/23 PHP
详谈PHP中的密码安全性Password Hashing
2017/02/04 PHP
php 中的closure用法详解
2017/06/12 PHP
javascript 日期常用的方法
2009/11/11 Javascript
解决jquery submit()提交表单提示:f[s] is not a function
2013/01/23 Javascript
extjs两个tbar问题探讨
2013/08/08 Javascript
js判断是否按下了Shift键的方法
2015/01/27 Javascript
JavaScript数据结构和算法之图和图算法
2015/02/11 Javascript
jQuery绑定事件on()与弹窗的简要概述
2016/04/27 Javascript
浅谈JavaScript的闭包函数
2016/12/08 Javascript
JavaScript中如何判断一个值的类型
2017/09/15 Javascript
Vue实现购物车场景下的应用
2017/11/27 Javascript
浅谈Vue-cli单文件组件引入less,sass,css样式的不同方法
2018/03/13 Javascript
vue将毫秒数转化为正常日期格式的实例
2018/09/16 Javascript
使用iView Upload 组件实现手动上传图片的示例代码
2018/10/01 Javascript
JS/HTML5游戏常用算法之路径搜索算法 随机迷宫算法详解【普里姆算法】
2018/12/13 Javascript
在LayUI图片上传中,解决由跨域问题引起的请求接口错误的方法
2019/09/24 Javascript
javascript实现前端分页效果
2020/06/24 Javascript
Openlayers3实现车辆轨迹回放功能
2020/09/29 Javascript
ES2020让代码更优美的运算符 (?.) (??)
2021/01/04 Javascript
[49:08]完美世界DOTA2联赛PWL S2 LBZS vs FTD.C 第一场 11.27
2020/12/01 DOTA
python3.5 + PyQt5 +Eric6 实现的一个计算器代码
2017/03/11 Python
利用python批量给云主机配置安全组的方法教程
2017/06/21 Python
Python调用微信公众平台接口操作示例
2017/07/08 Python
如何在python中使用selenium的示例
2017/12/26 Python
运用TensorFlow进行简单实现线性回归、梯度下降示例
2018/03/05 Python
tensorflow实现简单的卷积神经网络
2018/05/24 Python
用uWSGI和Nginx部署Flask项目的方法示例
2019/05/05 Python
Python Django 简单分页的实现代码解析
2019/08/21 Python
Python如何基于Tesseract实现识别文字功能
2020/06/05 Python
如何卸载python插件
2020/07/08 Python
深入解读CSS3中transform变换模型的渲染
2016/05/27 HTML / CSS
计算机系毕业生推荐信
2013/11/06 职场文书
党员“四风”方面存在问题及整改措施
2014/09/24 职场文书
golang DNS服务器的简单实现操作
2021/04/30 Golang