[转]页面关闭弹出提示并注销登录(兼容主流浏览器)
页面关闭弹出提示并注销登录(兼容主流浏览器)
这是多么普遍而又正常的需求啊,然而在多浏览器时代,这又是多么难做啊~~(我不是FE,我是Java工程师)
目前这个代码能够兼容以下浏览器(我亲测过的):
IE8,Chrome12,Firefox5,Safari4
应该也能支持以下浏览器:
IE7,Chrome8以后的版本,Firefox3.6,4
由于我们的系统不支持IE6,因此就不考虑这个问题了,估计应该没啥问题。
由于Opera不支持beforeunload事件,因此不会弹出提示让用户决定是否退出,同时对于刷新回退等操作也不会调用unlaod,因为它认为那是reload,会直接从cache中获得。我唯一成功响应Opera的unload事件是将Opera关闭了,然后再打开由于它会保留上次的标签,因此会重新加载,就在这个时候它响应了unload。因此放弃对Opera的支持,毕竟我们的服务对象没什么人用。
本来的逻辑是当用户关闭或者刷新或者后退时会弹出一个提示框询问用户是否真的关闭,这里面有以下几个技术陷阱:
1 这个事情只能通过beforeunload完成,但是一旦注册了这个事件,浏览器就会弹出个确认框,不需要你自己写什么confirm,我起初不知道,因为使用了一个开源的产品,找遍了它所有的代码想解除绑定或者屏蔽那个确认框,后来才发现原来是浏览器内置的!
其用法如下:
window.onbeforeunload=function(){ return 'Are you really going to leave?'; } <script language='javascript'> function getOs(){ if(navigator.userAgent.indexOf("MSIE")>0)return 1;//IE if(isFirefox=navigator.userAgent.indexOf("Firefox")>0)return 2;//Firefox if(isSafari=navigator.userAgent.indexOf("Chrome")>0)return 3;//Chrome if(isSafari=navigator.userAgent.indexOf("Safari")>0)return 4;//Safari if(isCamino=navigator.userAgent.indexOf("Camino")>0)return 5;//Camino if(isMozilla=navigator.userAgent.indexOf("Gecko/")>0)return 6;//Gecko if(isOpera=navigator.userAgent.indexOf('Opera') >= 0) return 7;//Opera //other... return 0; } var quite=false; function bindOnbeforeunload(){ quite=false; window.onbeforeunload=checkLeave; } function unbindOnbeforeunload(){ quite=true; window.onbeforeunload=null; } function checkLeave(){ return '您正在离开...'; } window.onunload=function(){ try{ unbindOnbeforeunload(); }catch(e){ } window.location.href='/Logout'; if(getOs()==3||getOs()==4){ var xmlHttp = false; try { xmlHttp = new ActiveXObject("Msxml2.XMLHTTP"); } catch (e) { try { xmlHttp = new ActiveXObject("Microsoft.XMLHTTP"); } catch (e2) { xmlHttp = false; } } if (!xmlHttp && typeof XMLHttpRequest != 'undefined') { xmlHttp = new XMLHttpRequest(); } var url = "/Logout"; xmlHttp.open("POST", url, false); // Send the request xmlHttp.send(null); window.open('', '_self', ''); //bug fix window.close(); } } </script> </head> <body oncontextmenu="return false;" onload="javascript:return bindOnbeforeunload();" >