Flex Javascript通信

4599 ワード

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" onload="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>