如果不希望用户点击浏览器的前进、后退、刷新、关闭等误操作,可以捕获这些事件,实际上是用js捕获window.onbeforeunload而已,如果你不希望通过修改html来达到这个效果,可以参考我的另一篇从flex-ifram项目中看到的,在as中写js函数中的代码,可以做到不需要修改flex编译成的html文件即可达到效果。而你只需要在你的mxml代码中加入如下内容即可:
- import flash.external.ExternalInterface;
- private static var FUNCTION_USEREXIT:String =
- "document.insertScript = function () " +
- "{ " +
- "window.onbeforeunload = function() " +
- "{ " +
- "var flexObj = MyTest2.checkExit(); " +//MyTest2是swf在html中object的id
- "if(flexObj != /"/") " +
- "{ " +
- "return flexObj; " +
- "}else{ " +
- "return; " +
- "} " +
- "} " +
- "} ";
import flash.external.ExternalInterface;
private static var FUNCTION_USEREXIT:String =
"document.insertScript = function () " +
"{ " +
"window.onbeforeunload = function() " +
"{ " +
"var flexObj = MyTest2.checkExit(); " +//MyTest2是swf在html中object的id
"if(flexObj != /"/") " +
"{ " +
"return flexObj; " +
"}else{ " +
"return; " +
"} " +
"} " +
"} ";
然后在Application的creationComplete方法中添加如下代码:
- ExternalInterface.call(FUNCTION_USEREXIT);
- ExternalInterface.addCallback("checkExit",checkExit);
- public function checkExit():String {
- var userExitStr:String = "你如果现在离开,则您的所有信息将失效!";
- return userExitStr;
- }