`
raymond.chen
  • 浏览: 1418626 次
  • 性别: Icon_minigender_1
  • 来自: 广州
社区版块
存档分类
最新评论

Flex与JavaScript之间的交互

阅读更多

一、mxml文件中的源码

<?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模板文件中添加以下代码:

<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')">

 

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics