actionscript与javascript交互实例(修改)

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"  
			   width="100%" height="100%" xmlns:code="http://code.google.com/p/flex-iframe/" 
			   creationComplete="initApp()"> 
	<fx:Declarations>
		<!-- 将非可视元素(例如服务、值对象)放在此处 -->
	</fx:Declarations>
	<fx:Script>
		<![CDATA[
			import flash.external.*;                   //引入ExternalInterface  
			public function initApp():void   
			{  
				ExternalInterface.addCallback("myJsFunction",myAsFuncn);  
				button.addEventListener(MouseEvent.CLICK,buttonClick);
			} 
			public function myAsFuncn(js:String):String  
			{  
				return  "js:"+js+"访问了as:"+asInput.text;  
			} 
			
			private function buttonClick(event:MouseEvent):void
			{
				trace(asInput.text);
				label.text = "as 调用 js ...";
				var result:String = callJS(asInput.text);
				label.text = "返回值:" + result;
			}
			
			private function callJS(arg:String):String
			{
				return ExternalInterface.call("jsMethod",arg);
			}
			private function asMethod(arg:String):String
			{
				label.text = arg;
				return "js 调用了 as 成功!";
			}
		]]>
	</fx:Script>
	<s:VGroup width="100%" height="100%">
		<s:VGroup width="100%">
			<mx:Button x="169.5" y="162" label="点击" fontSize="14" id="button"/>  
			<mx:TextInput  id="asInput" x="122" y="76"/>
			<s:Label id="label"/>
		</s:VGroup>
	</s:VGroup>
	<code:IFrame source="MyHtml.html" width="500" height="500"/>
</s:Application>

html页面:

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    <title>MyHtml.html</title>
	
    <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
    <meta http-equiv="description" content="this is my page">
    <meta http-equiv="content-type" content="text/html; charset=UTF-8">
    
    <!--<link rel="stylesheet" type="text/css" href="./styles.css">-->

   <script language="JavaScript">  
  
        function callAs( )  
        {  
            var myFlexfun=document.getElementById("myFlexFun");  
            var result=myFlexfun.myJsFunction(jsinput.value);  
            alert(result);  
            mess.value=result;
        }  
        
        function jsMethod(arg) {
			alert("as 调用 js 成功,参数为:" + arg);
			return ("as 调用 js 成功!");
		}
    </script> 
</head>
 <body>  
       <table>  
	        <tr>  
	           <td>   
					<object id= "myFlexFun" classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000" codebase="http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=6,0,29,0" width="400" height="300">   
						<param name="movie" value="test.swf" />   
						<param name="quality" value="high" />   
						<embed src="test.swf" quality="high" pluginspage="http://www.macromedia.com/go/getflashplayer" type="application/x-shockwave-flash" width="400" height="300"></embed>   
					</object> 
	          	</td>  
	        </tr>  
	        <tr>  
	           <td>
	          		<input type="input" id="jsinput"/>
	          		<input type="button" value="点我看效果" οnclick="callAs()"/>
	          		<input type="input" id="mess"/>
	           </td>  
	        </tr>   
        </table>  
    </body>  
</html>







如果试过以上的方法,会发现在被嵌入的Html见面中又嵌入了一个父级的swf文件,如下

这种情况显然不是我想要的结果。于是修改下代码,如下:

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"  
			   width="100%" height="100%" xmlns:code="http://code.google.com/p/flex-iframe/" 
			   creationComplete="initApp()"> 
	<fx:Declarations>
		<!-- 将非可视元素(例如服务、值对象)放在此处 -->
	</fx:Declarations>
	<fx:Script>
		<![CDATA[
			import flash.external.*;
			
			import mx.controls.Alert;
			import mx.rpc.events.ResultEvent;                   //引入ExternalInterface  
			public function initApp():void   
			{  
				ExternalInterface.addCallback("sayCallBack",callBack);  
			} 
			
			public function callBack(str:String):String{
				return "Hello "+str;
			}

			protected function button1_clickHandler(event:MouseEvent):void
			{
				ExternalInterface.call('sayHelloWorld',' Jim');
				var array:Array = new Array(); 
				array.push("a", "b","c","d","e"); 
				myIFrame.callIFrameFunction('jsMethod',array,function(str:String):void{
					label.text=str;
				});
			}
			
			public static function str():String{
				return "aaa";
			}

		]]>
	</fx:Script>
	<s:VGroup width="100%" height="100%">
		<s:BorderContainer width="100%" height="300" backgroundColor="#00ffaa">
			<s:HGroup width="100%">
				<s:Button label="调用js" click="button1_clickHandler(event)"/>
				<mx:Button x="169.5" y="162" label="点击" fontSize="14" id="button"/>  
				<mx:TextInput  id="asInput" x="122" y="76"/>
				<s:Label id="label"/>
			</s:HGroup>
		</s:BorderContainer>
		<s:BorderContainer width="100%" height="300">
			<code:IFrame id="myIFrame" source="com/MyHtml.html" width="100%" height="100%"/>
		</s:BorderContainer>
	</s:VGroup>
</s:Application>

html页面:

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    <title>MyHtml.html</title>
	
    <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
    <meta http-equiv="description" content="this is my page">
    <meta http-equiv="content-type" content="text/html; charset=UTF-8">
    
    <!--<link rel="stylesheet" type="text/css" href="./styles.css">-->
   <script language="JavaScript">  
  
        function callAs( )  
        {  
			var ifrm =window.parent.xiSwfUrlStr;
		    if (ifrm)
		    {
		        alert(ifrm);
		    }else{
		    	alert('false');
		    }
        }  
        
        function jsMethod(arr) {
			alert("as 调用 js 成功,参数为:"+arr[4]);
			return ("as 调用 js 成功!");
		}
        
        function callA(){
        	var str=window.parent.document.getElementById("test").sayCallBack("Bill");
        	if (str)
		    {
		        alert(str);
		        mess.value=str;
		    }else{
		    	alert('false');
		    }
        }
                
    </script> 
</head>

 <body>  
       <table>  
	        <tr>  
	           <td > 
	           		<object id= "myFlexFun" classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000"  width="100%" height="100%">   
						<param name="movie" value="../test.swf" />   
						<param name="quality" value="high" />   
						   
					</object>
	          	</td>  
	        </tr>  
	        <tr>  
	           <td>
	          		<input type="input" id="jsinput"/>
	          		<input type="button" value="点我看效果" οnclick="callA()"/>
	          		<input type="input" id="mess"/>
	           </td>  
	        </tr>   
        </table>  
    </body>  
</html>

这样,html的js脚本就可以直接调用as脚本,而不需要再次嵌入swf文件。






  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
目录 第 1 章 : 使用日期和时间 第 2 章 : 使用字符串 第 3 章 : 使用数组 第 4 章 : 处理错误 第 5 章 : 使用正则表达式 第 6 章 : 使用 XML 第 7 章 : 使用本机 JSON 功能 第 8 章 : 处理事件 第 9 章 : 使用应用程序域 第 10 章 : 显示编程 第 11 章 : 使用几何结构 第 12 章 : 使用绘图 API 第 13 章 : 使用位图 第 14 章 : 过滤显示对象 第 15 章 : 使用 Pixel Bender 着色器 第 16 章 : 使用影片剪辑 第 17 章 : 使用补间动画 第 18 章 : 使用反向运动 第 19 章 : 在三维 (3D) 环境中工作 第 20 章 : 文本使用基础知识 第 21 章 : 使用 TextField 类 第 22 章 : 使用 Flash 文本引擎 第 23 章 : 使用 Text Layout Framework 第 24 章 : 处理声音 第 25 章 : 使用视频 第 26 章 : 使用摄像头 第 27 章 : 使用数字权限管理 第 28 章 : 在 AIR 中添加 PDF 内容 第 29 章 : 用户交互的基础知识 第 30 章 : 键盘输入 第 31 章 : 鼠标输入 第 32 章 : 触摸、多点触控和手势输入 第 33 章 : 复制和粘贴 第 34 章 : 加速计输入 第 35 章 : AIR 中的拖放 第 36 章 : 使用菜单 第 37 章 : AIR 中的任务栏图标 第 38 章 : 使用文件系统 第 39 章 : 存储本地数据 第 40 章 : 在 AIR 中使用本地 SQL 数据库 第 41 章 : 使用字节数组 第 42 章 : 网络和通信基础知识 第 43 章 : 套接字 第 44 章 : HTTP 通信 第 45 章 : 与其他 Flash Player 和 AIR 实例通信 第 46 章 : 与 AIR 中的本机进程通信 第 47 章 : 使用外部 API 第 48 章 : AIR 中的 XML 签名验证 第 49 章 : 客户端系统环境 第 50 章 : AIR 应用程序的调用和终止 第 51 章 : 处理 AIR 运行时和操作系统信息 第 52 章 : 使用 AIR 本机窗口 第 53 章 : AIR 中的显示屏幕 第 54 章 : 打印 第 55 章 : Geolocation 第 56 章 : 应用程序国际化 第 57 章 : 本地化应用程序 第 58 章 : 关于 HTML 环境 第 59 章 : 在 AIR 中进行 HTML 和 JavaScript 编程 第 60 章 : 为 AIR HTML 容器编写脚本 第 61 章 : 处理 AIR 中与 HTML 相关的事件 第 62 章 : 在移动应用程序中显示 HTML 内容 第 63 章 : 安全性 第 64 章 : 如何使用 ActionScript 示例 第 65 章 : 本地数据库中的 SQL 支持 第 66 章 : SQL 错误详细消息、 ID 和参数 第 67 章 : Adobe 图形汇编语言 (AGAL)

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值