Flex Javascript 通信

 javascript调用Flex
      1.其中movieName 为<param name="movie" value="main.swf" />
          function thisMovie(movieName){
		if(navigator.appName.indexOf("Microsoft")!=-1){
			return window[movieName];
		}else{
			return document[movieName];
		}
          }       
             1. 获取flex对象,查找方法并调用      
                   thisMovie("main").callFromJavaScript(document.getElementById("value").value);

1.添加Javascript 可以访问Flex的资源到容器内!
2.其中第一个参数为JavaScript调用Flex资源的Flex资源别名
3.其中Flex的访问范围如private在javascript中也能访问

        ExternalInterface.addCallback("callFromJavaScript",callFromJavaScript2); 


  Flex调用javascript
          ExternalInterface.call("getFromFlex",valueForJavascript.text);

----------------------------------------------------------------------------------------------------------------------------------------------------------
<?xml version="1.0" encoding="utf-8"?>
<mx:Application 
xmlns:mx="http://www.adobe.com/2006/mxml"
xmlns:loowj="http://www.loowj.com/flex/upload" 
layout="absolute"
creationComplete="initApp()"
>
	<mx:Script>
		<![CDATA[
			import mx.controls.Alert;
			
			public function initApp():void{
				if(this.isJSReady){
					this.setupCallbacks();
				}else{
					var timer:Timer = new Timer(100);
					timer.addEventListener(TimerEvent.TIMER, jsNotReady);
					timer.start();
				}
			}
			
			//javascript 调用 flex
			public function callFromJavaScript(s:String):void{
				Alert.show(s,"提示");
			}
			
			private function callFromJavaScript2(s:String):void{
				Alert.show(s,"未经使用");
			}
			
			
			//flex 调用  javascript
			public function sendToJavaScript():void{
				ExternalInterface.call("getFromFlex",valueForJavascript.text);
			}
			
			//设置回调函数
			private function setupCallbacks():void{
				ExternalInterface.addCallback("callFromJavaScript",callFromJavaScript2);
			}
			
			private function isJSReady():Boolean{
				return ExternalInterface.call("isJSReady");
			}
			
			private function jsNotReady(event:TimerEvent):void{
				if(this.isJSReady()){
					Timer(event.target).stop();
					this.setupCallbacks();
				}
			}
		]]>
	</mx:Script>
	<mx:Panel title="ExternalInterface" width="350" height="200">
		<mx:HBox>
			<mx:TextInput id="valueForJavascript" text="Hello From flex" />
			<mx:Button click="this.sendToJavaScript()" label="send to javascript"/>
		</mx:HBox>
	</mx:Panel>
</mx:Application>
<html lang="en">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title></title>
<script type="text/javascript">
	function getFromFlex(s){
		alert("javascript:"+s);
	}
	
	function sendToFlex(){
		thisMovie("main").callFromJavaScript(document.getElementById("value").value);
	}
	
	function thisMovie(movieName){
		if(navigator.appName.indexOf("Microsoft")!=-1){
			return window[movieName];
		}else{
			return document[movieName];
		}
	}
	var jsReady = false;
	function init(){
		jsReady = true;
	}
</script>
</head>


<body scroll="no" οnlοad="init()">
	<h1>flex TO JavaScript</h1>
		<br>
		<div aligen="center">
			<from name="form1">
				<input id="value" type="text" name="valueForFlex" value="Hello From Javascript"/>
				<input type="button" onClick="sendToFlex()" value="Send to Flex"/>
			</from>
			<br/>
			<object classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000"
					id="main" width="300" height="200"
					codebase="http://fpdownload.macromedia.com/get/flashplayer/current/swflash.cab">
					<param name="movie" value="main.swf" />
					<param name="quality" value="high" />
					<param name="bgcolor" value="#869ca7" />
					<param name="allowScriptAccess" value="sameDomain" />
					<embed src="main.swf" quality="high" bgcolor="#869ca7"
						width="100%" height="100%" name="main" align="middle"
						play="true"
						loop="false"
						quality="high"
						allowScriptAccess="sameDomain"
						type="application/x-shockwave-flash"
						pluginspage="http://www.adobe.com/go/getflashplayer">
					</embed>
			</object>
		</div>
</body>


</html>


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值