flex与JavaScript交互
时间:2009-08-06 来源:integergx
- <?xml version="1.0" encoding="utf-8"?>
- <mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" layout="absolute" initialize="init()">
- <mx:Script>
- <![CDATA[
- private function init():void{
- if(ExternalInterface.available){
- //将AS方法注册为JS可以调用的方法
- ExternalInterface.addCallback("flexMethod", flexMethod);
- }
- }
- public function flexMethod(s:String):void{
- txt1.text = s;
- }
- private function callJavaScriptMethod():void{
- if(ExternalInterface.available){
- //在AS中调用JS的方法
- ExternalInterface.call("jsMethod", "MM");
- //ExternalInterface.call("window.location.reload");
- }
- }
- ]]>
- </mx:Script>
- <mx:Button x="27" y="28" label="Load" click="callJavaScriptMethod()"/>
- <mx:TextArea x="27" y="58" width="450" height="143" id="txt1"/>
- </mx:Application>
二、在/html-template/index.template.html模板文件中添加以下代码:
Html代码
- <script language="javascript">
- function callFlexMethod(text){
- //${application}.flexMethod(text);
- document.getElementById("${application}").flexMethod(text);
- }
- function jsMethod(text){
- alert("hello " + text);
- }
- </script>
- <input type=button value="click" onclick="callFlexMethod('ddddd')">
相关阅读 更多 +