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

flex(as) 与 html(js)的通信交互

发布时间:2020-12-15 04:44:20 所属栏目:百科 来源:网络整理
导读:??? 最近在研究FLEX与HTML的交互,基本的基础可以参照网络的js与as交互博客:http://blog.sina.com.cn/s/blog_6fb6c8e301011vuk.html; ?我就直接上传一个例子代码在此处好了;需要注意的是一个安全沙箱的问题,在html中嵌入flash的OBJECT时候,要设置:allo

??? 最近在研究FLEX与HTML的交互,基本的基础可以参照网络的js与as交互博客:http://blog.sina.com.cn/s/blog_6fb6c8e301011vuk.html;

?我就直接上传一个例子代码在此处好了;需要注意的是一个安全沙箱的问题,在html中嵌入flash的OBJECT时候,要设置:allowscriptaccess = always;详细可看例子即可;下图为具体的文件架构图:

下面分别给出系列代码

1、jsasTest.html

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    <title>test</title>
  </head>
  <script type="text/javascript">
  //发送请求
  var m=0;
  	function sendToAs(){
  		++m;
  		window.swf.callfromjs("js:"+m);
  	}
  	//接收请求
  	function callfromAs(msg){
  		document.getElementById("jslab").innerHTML = msg;
  	}
  	
  </script>
  <body>
  <object classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000"
              id="swf" width="100%" height="100%"
              codebase="http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab">
          <param name="movie" value="ASJS.swf" />
          <param name="quality" value="high" />
          <param name="bgcolor" value="#ffffff" />
          <param name="allowScriptAccess" value="always" />
          <embed src="ASJS.swf" quality="high" bgcolor="#869ca7"
              width="500" height="375" name="swf" align="middle"
              play="true" loop="false" quality="high" allowScriptAccess="always"
              type="application/x-shockwave-flash"
              pluginspage="http://www.macromedia.com/go/getflashplayer">
          </embed>
      </object>
  <br/>
  <label id="jslab" >JSmsg</label>
  <br/>
  <button id="jsbtn"  onclick="sendToAs();">BTN</button>
  </body>
</html>

2、ASJS.mxml

<?xml version="1.0" encoding="utf-8"?>
<s:Application xmlns:fx="http://ns.adobe.com/mxml/2009" 
			   xmlns:s="library://ns.adobe.com/flex/spark" 
			   xmlns:mx="library://ns.adobe.com/flex/mx" 
			   minWidth="300" minHeight="300"
			   creationComplete="int(event)"
			   >
	<s:layout>
		<s:VerticalLayout horizontalAlign="center" verticalAlign="middle"/>
	</s:layout>
	<fx:Script>
		<![CDATA[
			import mx.events.FlexEvent;
			[Bindable]
			private var num:Number = 0;
			
			//访问外部JS方法
			protected function sendToJs(event:MouseEvent):void
			{
				++num;
				ExternalInterface.call("callfromAs","call:"+this.num);
			}
			
			protected function int(event:FlexEvent):void
			{
				Security.allowDomain("*");
				ExternalInterface.addCallback("callfromjs",callfromjs_as);
			}
			//接收访问
			public function callfromjs_as(str:String):void{
				this.lab1.text = str;
			}
			
		]]>
	</fx:Script>
	
	<fx:Declarations>
		<!-- 将非可视元素(例如服务、值对象)放在此处 -->
	</fx:Declarations>
	<s:Button id="btn1" click="sendToJs(event)" label="BTN1"/>
	<s:Label text="{num}" backgroundColor="gray" id="lab1" width="60" height="40" textAlign="center" verticalAlign="middle"/>
</s:Application>


这里的mxml文件需要编译(Flash Builder)为swf文件后才能放到相应的服务器里面运行;

?

注意:flash 与 html的访问需要放置在服务器中运行,单独的文件系统是会出现“安全沙箱”问题的;


<?xml version="1.0" encoding="utf-8"?>
<s:Application xmlns:fx="http://ns.adobe.com/mxml/2009" 
			   xmlns:s="library://ns.adobe.com/flex/spark" 
			   xmlns:mx="library://ns.adobe.com/flex/mx" 
			   minWidth="300" minHeight="300"
			   creationComplete="int(event)"
			   >
	<s:layout>
		<s:VerticalLayout horizontalAlign="center" verticalAlign="middle"/>
	</s:layout>
	<fx:Script>
		<![CDATA[
			import mx.events.FlexEvent;
			[Bindable]
			private var num:Number = 0;
			
			//访问外部JS方法
			protected function sendToJs(event:MouseEvent):void
			{
				++num;
				ExternalInterface.call("callfromAs",callfromjs_as);
			}
			//接收访问
			public function callfromjs_as(str:String):void{
				this.lab1.text = str;
			}
			
		]]>
	</fx:Script>
	
	<fx:Declarations>
		<!-- 将非可视元素(例如服务、值对象)放在此处 -->
	</fx:Declarations>
	<s:Button id="btn1" click="sendToJs(event)" label="BTN1"/>
	<s:Label text="{num}" backgroundColor="gray" id="lab1" width="60" height="40" textAlign="center" verticalAlign="middle"/>
</s:Application>

(编辑:李大同)

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

    推荐文章
      热点阅读