JavaScript 调用 ActionScript

本文介绍了一个使用MXML和ActionScript实现的示例,展示了如何在Flash与JavaScript之间进行数据传递。通过设置允许跨域访问及注册点击事件,当用户点击按钮时,可以将HTML页面中的文本区域内容发送到Flash中并在Flash内部显示。

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

MXML:
<? xml version="1.0" encoding="utf-8" ?>

< mx:Application  xmlns:mx ="http://www.adobe.com/2006/mxml"  xmlns ="*"
    creationComplete
="onCreationComplete()" >

    
< mx:Script  source ="ExternalInterfaceJSAS.as"   />

    
< mx:Canvas  width ="100%"  height ="100%" >
        
< mx:Panel  x ="0"  y ="0"  height ="300"  width ="500"  title ="ExternalInterface : JavaScript to Flash" >
            
< mx:Canvas  height ="100%"  width ="100%" >
                
< mx:TextArea  x ="8"  y ="9"  width ="464"  height ="250"  id ="tArea" />
            
</ mx:Canvas >
        
</ mx:Panel >
    
</ mx:Canvas >
</ mx:Application >
 
ExternalInterfaceJSAS.as:

import flash.events.MouseEvent;
import flash.external.ExternalInterface;
import flash.system.Security;

private 
function  onCreationComplete(): void
{
    Security.allowDomain(
"*");
    submitButton.addEventListener(
"click", onSubmitClick);
}


private 
function  onSubmitClick(event:MouseEvent): void
{
    
//we need to make sure that ExternalInterface is available
    //If you are running in StandAlone player, then this will return false
    if(ExternalInterface.available)
    
{
        
var s:String = tInput.text;
        
        
//call the javascript function
        ExternalInterface.call("displayString", s);
    }

}


HTML:
< html >
< head >
< title > ExternalInterfaceJSAS </ title >
< style >
body 
{ margin: 0px;
 overflow
:hidden }

</ style >

< script  language ="JavaScript" >

    
//called when send button is pressed
    function doSubmit()
    
{
        
//get the data from the TextArea
        var ta = document.getElementById("outputField");
        sendToFlash(ta.value);
    }


    
function sendToFlash(input)
    
{
        
//get access to the flash player in the page with an ID of fp
        var flash = (navigator.appName.indexOf ("Microsoft"!=-1)?window["fp"]:document["fp"];
        
        
//call the function in flash
        flash.displayString(input);
    }

</ script >


</ head >
< body  scroll ='no' >
< object  id ='fp' 
    
classid ='clsid:D27CDB6E-AE6D-11cf-96B8-444553540000'
    
codebase ='http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=8,5,0,0'
    
height ='300'  width ='450' >
     
    
< param  name ='flashvars'  />
    
< param  name ='src'  value ='ExternalInterfaceJSAS.swf' />
    
    
< embed  name ='fp'
    
pluginspage ='http://www.macromedia.com/go/getflashplayer'
    
allowScriptAccess ='always'
    
src ='ExternalInterfaceJSAS.swf'  height ='300'
    
width ='500'  />
</ object >

< form >
    
< textarea  id ="outputField"  style ="width:450px;height:50px" ></ textarea >
    
< br  />
    
< input  type ="button"  value ="Send to Flash"  onclick ="doSubmit()" />
</ form >
</ body >
</ html >
    

需要注意的地方是不能直接打开html,需要放在服务器上运行,如Tomcat上
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值