Flex中捕获浏览器的前进、后退、刷新、关闭事件

如果不希望用户点击浏览器的前进、后退、刷新、关闭等误操作,可以捕获这些事件,实际上是用js捕获window.onbeforeunload而已,如果你不希望通过修改html来达到这个效果,可以参考我的另一篇从flex-ifram项目中看到的,在as中写js函数中的代码,可以做到不需要修改flex编译成的html文件即可达到效果。而你只需要在你的mxml代码中加入如下内容即可:

Java代码 复制代码
  1. import flash.external.ExternalInterface;   
  2. private static var FUNCTION_USEREXIT:String =    
  3.     "document.insertScript = function () " +   
  4.     "{ " +   
  5.         "window.onbeforeunload = function() " +   
  6.         "{ " +   
  7.             "var flexObj = MyTest2.checkExit(); " +//MyTest2是swf在html中object的id   
  8.             "if(flexObj != /"/") " +   
  9.             "{ " +   
  10.                 "return flexObj; " +   
  11.             "}else{ " +   
  12.                 "return; " +   
  13.             "} " +   
  14.         "} " +   
  15.     "} ";  
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方法中添加如下代码:

Java代码 复制代码
  1. ExternalInterface.call(FUNCTION_USEREXIT);   
  2. ExternalInterface.addCallback("checkExit",checkExit);   
  3. public function checkExit():String {   
  4.       var userExitStr:String = "你如果现在离开,则您的所有信息将失效!";   
  5.       return userExitStr;   
  6. }  
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值