网页javascript 与flash之间的交互

来源:互联网 发布:java高级培训 编辑:程序博客网 时间:2024/05/16 19:42

构建JS交互接口类

package com.Interactive 

{

public class JSInterface
{

public function JSInterface(){

//register javascript function
if (ExternalInterface.available) 
{
try
{
//ExternalInterface.marshallExceptions = true;
timeOutExternallAddBack("sendToFlash",recvFromJS);

}
catch (error:SecurityError)
{
throw new Error("A SecurityError occurred: " + error.message + "\n");
}
catch (error:Error)
{
throw new Error("An Error occurred: " + error.message + "\n");
}
}
else 
{
throw new Error("External interface is not available for this container.");
}

}


public function recvResponseFromFlash(msg:Object):void
{
try
{
timeOutExternallCall("recvResponseFromFlash", msg);

catch(e:Error)
{
throw e;
}
}

public function recvFromJS(msg:Object):void
{

}


/**
* 延时呼叫
* @param func
* @param msg

*/
public function timeOutExternallCall(func:String,msg:Object):void
{
setTimeout(function():void{ExternalInterface.call(func,msg);},500);
// timeOutExternallCall(func,msg);
}
/**
* 延时监听
* @param func
* @param msg

*/
public function timeOutExternallAddBack(func:String,closure:Function):void
{
setTimeout(function():void{ExternalInterface.addCallback(func,closure)},500);
}

}

}


如上:我们注册了recvFromJS接口函数,用于响应外部javascript调用sendToFlash方法。

注册recvResponseFromFlash接口函数,在flash内部使用时,javascript代码中需定义recvResponseFromFlash函数,用于接收flash传递给javascript的消息。


需要使用该交互类时,如下:

var inter:JSInterface = new JSInterface();即可。


0 0
原创粉丝点击