帝王谷资源网 Design By www.wdxyy.com
原理就是通过离开页面行为时间onunload触发时间去检测此时的浏览器的窗口大小,根据大小由此判断用户是刷新,跳转或是关闭行为程序
代码如下
window.onunload = function(){ var a_n = window.event.screenX - window.screenLeft; var a_b = a_n > document.documentElement.scrollWidth-20; if(a_b && window.event.clientY< 0 || window.event.altKey){ alert('关闭页面行为'); }else{ alert('跳转或者刷新页面行为'); } }
用浏览器右上角的关闭按钮时好用,但在选项卡上关闭和在任务栏上关闭,这个方法就不作用了
js标签只有onloadonunloadonbeforeunload事件,而没有onclose事件。
不管页面是关闭还是刷新都会执行onunload事件。
如何捕捉到页面关闭呢?
页面加载时只执行onload
页面关闭时只执行onunload
页面刷新时先执行onbeforeunload,然后onunload,最后onload。
这样我们可以在onbeforeunload中加一个标记,在onunload中判断该标记,即可达到判断页面是否真的关闭了
更完整的兼容ff
代码如下
<mce:script type="text/javascript"><!-- function close(evt) //author: sunlei { var isIE=document.all"是关闭而非刷新"); window.location.href="../include/logout.php"; } else{ //alert("是刷新而非关闭"); return false; } } else{//火狐浏览器 if(document.documentElement.scrollWidth!=0) { //alert("是刷新而非关闭"); //window.location.href="report_list.php"; return false; } else{ alert("是关闭而非刷新"); //window.location.href="repost_list.php"; //alert("bbbbbbb"); } } } // --></mce:script> <BODY onunload="close(event);">
上面的方法没办法判断多选项卡的浏览器,如360,ie8这种,下面再看
代码如下
function CloseOpen(event) { if(event.clientX<=0 || event.clientY<0) { //获取当前时间 var date=new Date(); //将date设置为过去的时间 alert("关闭网页"); date.setTime(date.getTime()-10000); //将userId这个cookie删除 document.cookie="zhuangtao;expire="+date.toUTCString(); document.cookie="quanxianzifucuan;expire="+date.toUTCString(); document.cookie="quanxian;expire="+date.toUTCString(); s0 += "关闭窗口!"; sw = 1; onbeforeunload(); // window.event.returnValue = '关闭浏览器将退出系统.'; } else { alert("刷新或离开"); } } var currentKeyCode = -1; function document.onkeydown() { // 本窗口的所有下属页面都必须含有本函数 top.currentKeyCode = event.keyCode; } function onbeforeunload(){ var sw = 0, s0 = ""; if (currentKeyCode == 116) { s0 += "刷新窗口!(F5)"; } else { if ((event.altKey)&&(currentKeyCode == 115)) { s0 += "关闭窗口!(alt+F4)"; sw = 1; //获取当前时间 var date=new Date(); //将date设置为过去的时间 alert("关闭窗口"); date.setTime(date.getTime()-10000); //将userId这个cookie删除 document.cookie="zhuangtao;expire="+date.toUTCString(); document.cookie="quanxianzifucuan;expire="+date.toUTCString(); document.cookie="quanxian;expire="+date.toUTCString(); } else { if ((event.clientX > 0)&&(event.clientX < document.body.clientWidth)) { s0 += "刷新窗口!"; } else { //获取当前时间 var date=new Date(); //将date设置为过去的时间 alert("关闭网页"); date.setTime(date.getTime()-10000); //将userId这个cookie删除 document.cookie="zhuangtao;expire="+date.toUTCString(); document.cookie="quanxianzifucuan;expire="+date.toUTCString(); document.cookie="quanxian;expire="+date.toUTCString(); s0 += "关闭窗口!"; sw = 1; } } } if (sw == 1) { event.returnValue = ""; } else { currentKeyCode = -1; } } <body onunload="CloseOpen(event)" ></body></html>
上面只有不能使用在任务栏关闭了,基本可以满足我们的要求了。
以上内容就是本文给大家介绍的javascript判断网页是关闭还是刷新,希望大家喜欢。
帝王谷资源网 Design By www.wdxyy.com
广告合作:本站广告合作请联系QQ:858582 申请时备注:广告合作(否则不回)
免责声明:本站文章均来自网站采集或用户投稿,网站不提供任何软件下载或自行开发的软件! 如有用户或公司发现本站内容信息存在侵权行为,请邮件告知! 858582#qq.com
免责声明:本站文章均来自网站采集或用户投稿,网站不提供任何软件下载或自行开发的软件! 如有用户或公司发现本站内容信息存在侵权行为,请邮件告知! 858582#qq.com
帝王谷资源网 Design By www.wdxyy.com
暂无评论...
更新日志
2024年10月25日
2024年10月25日
- 群星2013-青春缤纷辑压箱宝大公开3CD2[新加坡限量版][WAV整轨]
- 林育群.2013-BalladShow(日本版)【环球】【WAV+CUE】
- 陈加洛.1992-痛到感觉不到【宝丽金】【WAV+CUE】
- 群星.2023-宿命之敌电视剧原声带【韶愔音乐】【FLAC分轨】
- 東京事変-大発見[FLAC+CUE]
- 椎名林檎-三文ゴシップ[FLAC+CUE]
- 2024年08月04日
- 裘德《裘德「最后的水族馆」演唱会LIVE》[320K/MP3][228.89MB]
- 裘德《裘德「最后的水族馆」演唱会LIVE》[24bit 48kHz][FLAC/分轨][2.08G]
- 基因三重奏《如果你什么都不说 音乐会现场录音》[320K/MP3][145.37MB]
- 孟庭苇.1996-月亮说话(2020环球24KGOLD限量版)【上华】【WAV+CUE】
- 群星.1997-新艺宝优质音响系列·国语精选监听版【新艺宝】【WAV+CUE】
- 阿桑.2005-寂寞在唱歌(星外星引进版)【华研国际】【WAV+CUE】
- 基因三重奏《如果你什么都不说 音乐会现场录音》[FLAC/分轨][287.43MB]
- 蔡题谦《我爱你,却依然要看你走》[320K/MP3][88.65MB]