加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

Flex Javascript 通信

发布时间:2020-12-15 01:18:18 所属栏目:百科 来源:网络整理
导读: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对象,查找方法
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>

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读