JS调用Flash和Flash调用JS

本文介绍了一个JavaScript与ActionScript交互的示例项目。通过使用swfobject.js嵌入Flash内容,并借助ExternalInterface实现JS调用AS及AS调用JS的功能。演示了如何在网页中设置Flash内容区域,并通过按钮触发Flash中的警告框。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

首先下载swfobject.js(http://code.google.com/p/swfobject/downloads/list)

可以使用FlashDevelop编译as文件(注:编译时选择Release,不要选择Debug,因为Debug会把一些没用的文件编译进swf文件)

 

as3 code:

package
{
 import flash.display.Sprite;
 import flash.events.*;
 import flash.net.FileReferenceList;
 import flash.net.FileFilter;
 
 import flash.external.ExternalInterface;
 
 public class Main extends Sprite
 {
  var str:String = "'js call flash function and flash call js function!'";

  public function Main() {
   try {
    ExternalInterface.addCallback("flAlert", this.showAlert);
   } catch (ex:Error) {
    return;
   }
  }
  public function showAlert() {
   ExternalInterface.call("jsAlert("+str+")");
  }
 }
 
}

 

html code:


<html>
<head>
 <title>Untitled</title>
 <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
 <script type="text/javascript" src="swfobject.js"></script>
 <script type="text/javascript">    
 swfobject.embedSWF("helloworld.swf", "myContent", "300", "120", "9.0.0"); 
 function jsAlert(v) {
  alert(v);
 }

 function callExternal() {
  document.getElementById("myContent").flAlert();
 }
 </script>
</head>
<body>
 <input type="button" onClick="callExternal()" value="Call Flash">
<div id="myContent">      
 <p>Alternative content</p>    
</div>
</body>
</html>

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值