如果想用Flash/Flex监听浏览器的关闭事件,
可以通过JavaScript的window.onbeforeunload事件进行监听
然后JS调用Flash中的函数。
在swf所在页面的JavaScript中添加如下代码
JS中代码:(这里设定swf文件名为TestFlash)
window.onbeforeunload = onbeforeunloadHandler; //添加响应函数
function onbeforeunloadHandler()
{
var swfRef = document.TestFlash|| window.TestFlash; //获取swf的引用
if ( swfRef ) {
warning = swfRef.windowCloseHandler(); // 调用Flash中的windowCloseHandler函数
return “Are you sure to close this page?”;
}
}
AS中代码:(在程序初始化的函数中添加,例如Flex的creationComplete事件中)
if(flash.external.ExternalInterface.available){ flash.external.ExternalInterface.addCallback(‘windowCloseHandler’,externalWindowCloseHandler);
//使用ExternalInterface向JS中添加调用函数
}
/*
* 告诉服务器,该flash已经被关闭
*/
protected function externalWindowCloseHandler():void{
//use HttpService in Flex
var http:HTTPService = new HTTPService();
http.url = ‘http://localhost/testphp/index.php?from=flexcloseByHTTPService’;
http.send();
//use URLLoader in AS3
var request:URLRequest = new URLRequest(‘http://localhost/testphp/index.php? from=flexcloseByUrlLoader’);
var urlLoader:URLLoader = new URLLoader();
urlLoader.load(request);
}
Flex中可以在FlashBuilder的HTML模板上添加JS代码
修改html-template中的index.template.html文件
在其中添加JS代码:
window.onbeforeunload = onbeforeunloadHandler; //添加响应函数
function onbeforeunloadHandler()
{
var swfRef = document.${application}|| window.${application}; //获取swf的引用
if ( swfRef ) {
warning = swfRef.windowCloseHandler(); // 调用Flash中的windowCloseHandler函数
return “Are you sure to close this page?”;
}
}
在AS中直接注入JS代码
如果不想更改HTML文件,也可以在AS中直接书写JS代码,注入到HTML文档中
if(flash.external.ExternalInterface.available){
var jsStr:String;
jsStr =
‘eval(\’window.onbeforeunload = onbeforeunloadHandler;’ +
‘function onbeforeunloadHandler(){‘ +
‘var swfRef = document.’+FlexGlobals.topLevelApplication.className+’ || window.’+FlexGlobals.topLevelApplication.className+’;’ +
‘swfRef.windowCloseHandler();’ +
‘return “Are you sure to close this page?”;’ +
‘}\’)';
flash.external.ExternalInterface.call(jsStr);
flash.external.ExternalInterface.addCallback(‘windowCloseHandler’,externalWindowCloseHandler);
}
移除该监听
只要设置window.onbeforeunload=null即可
AS中可以这样写
flash.external.ExternalInterface.call(‘eval(\’window.onbeforeunload = null\’)');
flash.external.ExternalInterface.call(‘eval(\’location.reload();\’)'); //再执行刷新浏览器的命令
由 Jack 撰写
From - http://blog.ityao.com/archives/581
分享到:
相关推荐
flex监听浏览器关闭事件,完成对于浏览器关闭事件的监听。以及弹出相应的关闭提示
附件是关于 Flash/Flex 几个重要框架 Cairngorm、Mate、PureMVC以及Swiz 的典型例子,由 Tony Hillerson 提供 Homepage: http://insideria.com
flash/flex画曲线,绘图板,用代码实现的个画曲线功能,类绘图板,可下载看看,不错的学习案例。
flash/flex 的aqua皮肤.类似苹果系统
FDT-flash/flex devtoolkit for eclipse. for最新版的 FDT 3.5 beta2
来自《潮汕IT男》网站的文章《解决flash/flex/as3 访问中文域名时的流错误》代码示例,地址:http://chenlinsheng.com/?p=990
NULL 博文链接:https://as3.iteye.com/blog/1134453
flex4浏览器关闭。一个简单的flex4中如果关闭浏览器的小demo!
本人用FLEX写的直播播放器,基于RTMP协议。代码要放入tomcat等容器里运行,打开play.html即可看到例子效果,含有源代码,希望能帮助到需要的人。
flex解析浏览器地址,。与浏览器的通信能够让 你建立一个可以超越Flex 应用本身的应用程序。你可以连接到已有的地址,通过JavaScript 和其他应用程序通信,并且可以和浏览器的历史记录交互,作为开始。...
终极破解方法,超级好用,100%成功,亲测,请大家使用。(会弹出个小框提示配置错误,但点击确定即可使用,没有任何影响)
NULL 博文链接:https://as3.iteye.com/blog/1454896
NULL 博文链接:https://as3.iteye.com/blog/975735
NULL 博文链接:https://as3.iteye.com/blog/909512
一.flex2的全屏代码 ...(参考:http://www.adobe.com/devnet/flashplayer/articles/full_screen_mode.html),下载了templates for Flex Builder,覆盖到flex项目的html-template目录下。全屏代码可以正常运行了。
flex事件flex事件flex事件flex事件flex事件
全面介绍yacc/flex的使用及原理,很实用
编译原理,使用c++/bison/flex实现linux下命令gerep
flex实现简单浏览器,flex AIR中HTML控件实现了简单的网页浏览效果
基于Flash 或 Flex:应用程序根内容(理解为容器)为Flash/Flex (SWF)。 基于HTML,应用程序根内容为HTML, JS, CSS。 基于HTML的Flash/Flex或PDF,应用程序根内容为基于HTML 的Flash/Flex (SWF) 或 PDF。 Adobe...