Javascript 调用 ActionScript 的简单方法
1.在Flex中,ActionScript调用Javascript是比较简单的,说白了就是,在html里,怎么调用Javascript,在ActionScript就怎么调用就可以了
2.如果用js调用as,就稍微麻烦一点,其实也比较简单
MXML代码:
<?xmlversion="1.0"encoding="utf-8"?> <mx:Applicationxmlns:mx="http://www.adobe.com//mxml"layout="vertical"horizontalAlign="left"backgroundColor="white" initialize="init()"> <mx:Labeltext="城市名称:"/> <mx:Listid="cityList"width=""height=""dataProvider="{cities}"/> <mx:ArrayCollectionid="cities"> <mx:String>北京</mx:String> <mx:String>上海</mx:String> </mx:ArrayCollection> <mx:Script> <![CDATA[ privatefunctioninit():void { //注册回调函数供JavaScript调用 ExternalInterface.addCallback("callActionScript",asFunctionByJs); } privatefunctionasFunctionByJs(city:String):void { cities.addItem(city); } ]]> </mx:Script> </mx:Application>
HTML代码(这些代码都是flexbuilder自动生成的,用于将flash嵌入到网页里,不用仔细看这些代码,注意黄色背景的部分,这是关键部分,是我加入到)
<!--savedfromurl=(0014)about:internet--> <htmllang="en"> <!-- SmartdevelopersalwaysViewSource. ThisapplicationwasbuiltusingAdobeFlex,anopensourceframework forbuildingrichInternetapplicationsthatgetdeliveredviathe FlashPlayerortodesktopsviaAdobeAIR. LearnmoreaboutFlexathttp://flex.org //--> <head> <metahttp-equiv="Content-Type"content="text/html;charset=utf-"/> <!--BEGINBrowserHistoryrequiredsection--> <linkrel="stylesheet"type="text/css"href="history/history.css"/> <!--ENDBrowserHistoryrequiredsection--> <title></title> <scriptsrc="AC_OETags.js"language="javascript"></script> <!--BEGINBrowserHistoryrequiredsection--> <scriptsrc="history/history.js"language="javascript"></script> <!--ENDBrowserHistoryrequiredsection--> <style> body{}{margin:px;overflow:hidden} </style> <scriptlanguage="JavaScript"type="text/javascript"> <!-- //----------------------------------------------------------------------------- //Globals //MajorversionofFlashrequired varrequiredMajorVersion=9; //MinorversionofFlashrequired varrequiredMinorVersion=0; //MinorversionofFlashrequired varrequiredRevision=124; //----------------------------------------------------------------------------- //--> </script> <scripttype="text/javascript"> functioncallActionScript(value) { //根据id获取flash实例,在这里id是CallAsFromJs,可以从Embed varflash=(navigator.appName.indexOf("Microsoft")!=-)?window["CallAsFromJs"]:document["CallAsFromJs"]; //调用ActionScript注册的回调方法 flash.callActionScript(value); } </script> </head> <bodyscroll="no"> 输入城市名称:<inputtype="text"id="newCityName"/><inputtype="button"value="添加城市"onclick="callActionScript(newCityName.value);"/> <scriptlanguage="JavaScript"type="text/javascript"> <!-- //VersioncheckfortheFlashPlayerthathastheabilitytostartPlayerProductInstall(6.0r65) varhasProductInstall=DetectFlashVer(6,0,65); //Versioncheckbaseduponthevaluesdefinedinglobals varhasRequestedVersion=DetectFlashVer(requiredMajorVersion,requiredMinorVersion,requiredRevision); if(hasProductInstall&&!hasRequestedVersion){ //DONOTMODIFYTHEFOLLOWINGFOURLINES //Locationvisitedafterinstallationiscompleteifinstallationisrequired varMMPlayerType=(isIE==true)?"ActiveX":"PlugIn"; varMMredirectURL=window.location; document.title=document.title.slice(0,47)+"-FlashPlayerInstallation"; varMMdoctitle=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" ); }elseif(hasRequestedVersion){ //ifwe'vedetectedanacceptableversion //embedtheFlashContentSWFwhenalltestsarepassed 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{//flashistoooldorwecan'tdetecttheplugin varalternateContent='AlternateHTMLcontentshouldbeplacedhere.' +'ThiscontentrequirestheAdobeFlashPlayer.' +'<ahref=http://www.adobe.com/go/getflash/>GetFlash</a>'; document.write(alternateContent);//insertnon-flashcontent } //--> </script> <noscript> <objectclassid="clsid:DCDBE-AED-cf-B-" id="CallAsFromJs"width="%"height="%" codebase="http://fpdownload.macromedia.com/get/flashplayer/current/swflash.cab"> <paramname="movie"value="CallAsFromJs.swf"/> <paramname="quality"value="high"/> <paramname="bgcolor"value="#ffffff"/> <paramname="allowScriptAccess"value="sameDomain"/> <embedsrc="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的简单方法,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对毛票票网站的支持!