[轉]Flash/Flex监听浏览器的关闭事件
FROM : http://blog.ityao.com/archives/581
?
?
如果想用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.οnbefοreunlοad=null即可
AS中可以這樣寫
flash.external.ExternalInterface.call(‘eval(\’window.onbeforeunload = null\’)');
flash.external.ExternalInterface.call(‘eval(\’location.reload();\’)');??????? //再執行刷新瀏覽器的命令
源文件 :TestFlex.zip
From - http://blog.ityao.com/archives/581
?
?
轉載于:https://www.cnblogs.com/Athrun/archive/2010/11/08/1871604.html
總結
以上是生活随笔為你收集整理的[轉]Flash/Flex监听浏览器的关闭事件的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: AppDelegate
- 下一篇: 【CSDN软件工程师能力认证学习精选】P