Javascript 调用 ActionScript 的简单方法
网络编程 2021-07-04 20:00www.168986.cn编程入门
在Flex中,ActionScript调用Javascript是比较简单的,说白了就是,在html里,怎么调用Javascript,在ActionScript就怎么调用就可以了。接下来通过本文给大家介绍js 调用 actionscript方法,感兴趣的朋友一起看看吧
1. 在Flex中,ActionScript调用Javascript是比较简单的,说白了就是,在html里,怎么调用Javascript,在ActionScript就怎么调用就可以了
2. 如果用js调用as,就稍微麻烦一点,其实也比较简单
MXML代码
<?xml version="1.0" encoding="utf-8"?> <mx:Application xmlns:mx="http://.adobe.//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:inter --> <html lang="en"> <!-- Smart developers always View Source. This application was built using Adobe Flex, an open source framework for building rich Inter applications that get delivered via the Flash Player or to desks via Adobe AIR. Learn more about Flex at http://flex. // --> <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 plete 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", "allowScriptAess","sameDomain", "type", "application/x-shockwave-flash", "pluginspage", "http://.adobe./go/getflashplayer" ); } else if (hasRequestedVersion) { // if we've detected an aeptable 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", "allowScriptAess","sameDomain", "type", "application/x-shockwave-flash", "pluginspage", "http://.adobe./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://.adobe./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./get/flashplayer/current/swflash.cab"> <param name="movie" value="CallAsFromJs.swf" /> <param name="quality" value="high" /> <param name="bgcolor" value="#ffffff" /> <param name="allowScriptAess" value="sameDomain" /> <embed src="CallAsFromJs.swf" quality="high" bgcolor="#ffffff" width="%" height="%" name="CallAsFromJs" align="middle" play="true" loop="false" quality="high" allowScriptAess="sameDomain" type="application/x-shockwave-flash" pluginspage="http://.adobe./go/getflashplayer"> </embed> </object> </noscript> </body> </html>
,js调用as,大概分为3步
1.as使用ExternalInterface.addCallback注册回调函数
2.在js函数中根据flash在网页中的id获取实例
3.用上面获取到flash实例,调用as的函数
以上所述是长沙网络推广给大家介绍的Javascript 调用 ActionScript 的简单方法,希望对大家有所帮助,如果大家有任何疑问请给我留言,长沙网络推广会及时回复大家的。在此也非常感谢大家对狼蚁SEO网站的支持!
编程语言
- 如何快速学会编程 如何快速学会ug编程
- 免费学编程的app 推荐12个免费学编程的好网站
- 电脑怎么编程:电脑怎么编程网咯游戏菜单图标
- 如何写代码新手教学 如何写代码新手教学手机
- 基础编程入门教程视频 基础编程入门教程视频华
- 编程演示:编程演示浦丰投针过程
- 乐高编程加盟 乐高积木编程加盟
- 跟我学plc编程 plc编程自学入门视频教程
- ug编程成航林总 ug编程实战视频
- 孩子学编程的好处和坏处
- 初学者学编程该从哪里开始 新手学编程从哪里入
- 慢走丝编程 慢走丝编程难学吗
- 国内十强少儿编程机构 中国少儿编程机构十强有
- 成人计算机速成培训班 成人计算机速成培训班办
- 孩子学编程网上课程哪家好 儿童学编程比较好的
- 代码编程教学入门软件 代码编程教程