网页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();即可。
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();即可。
相关文章推荐
- 【ActionScript】Flash与网页的交互,ActionScript与JavaScript的交互
- Flash, JavaScript, PHP之间的交互
- 【ActionScript】Flash与网页的交互,ActionScript与JavaScript的交互
- 积少成多Flash(8) - ActionScript 3.0 网页之获取参数,JavaScript与ActionScript之间的相互调用
- 积少成多Flash(8) - ActionScript 3.0 网页之获取参数,JavaScript与ActionScript之间的相互调用
- 积少成多Flash(8) - ActionScript 3.0 网页之获取参数,JavaScript与ActionScript之间的相互调用
- javaScript事件响应,让网页交互
- JavaScript与 HTML表单的交互过程,想要学习动态网页但是无从下手的新手看看。
- 网页中ActiveX控件与JavaScript交互 js调用ActiveX函数
- (转)flash和javascript之间的通讯
- javascript和Flash ActionScript的交互(AS3)
- JavaScript的事件响应与网页交互
- javascript和swf在网页中交互的一些总结
- JavaScript事件流 HTML和CSS代码支持页面的外观,JavaScript代码支持页面的行为,而JavaScript与HTML之间的交互是通过事件实现的。事件,是文档或者浏览器窗口中发生
- 稳扎稳打Silverlight(27) - 2.0网页之可脚本化, 与DOM的交互, 与JavaScript的交互
- HTML网页与FLASH之间的“静态”传值
- 7款Flash和Javascript网页视频播放器
- php数据库的增删改查 php与javascript之间的交互
- ActiveX控件与Javascript之间的交互
- PHP与FLASH之间的交互