您的位置:首页 > Web前端 > JavaScript

网页javascript 与flash之间的交互

2015-05-04 14:52 453 查看
构建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();即可。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: