js判断页面是关闭还是刷新

JavaScript011

js判断页面是关闭还是刷新,第1张

<mce:script type="text/JavaScript"><!--function close(evt) //author: sunlei{var isIE=document.all?true:falseevt = evt ? evt :(window.event ? window.event : null)if(isIE){//IE浏览器var n = evt.screenX - window.screenLeftvar b = n >document.documentElement.scrollWidth-20if(b &&evt.clientY<0 || evt.altKey){//alert("是关闭而非刷新")window.location.href="../include/logout.PHP"}else{//alert("是刷新而非关闭")return false}}else{//火狐浏览器if(document.documentElement.scrollWidth!=0){//alert("是刷新而非关闭")//window.location.href="report_list.php?ss=1"return false}else{alert("是关闭而非刷新")//window.location.href="repost_list.php?ss=0"//alert("bbbbbbb")}}}// --></mce:script><BODY onunload="close(event)">

首先判断浏览器的类型,简便可用navigator.userAgent()获取浏览器的字符串,与浏览器类型做查找即可。 目前对Chrome和firfox区分关闭和刷新成功。 浏览器为firfox时flag为false,Chrome为true。window.onload(){window.onunload = function() {if(flag){console.log('关闭操作')}else {console.log('刷新操作')}}window.onbeforeunload = function () {if(!flag){console.log('关闭操作')}else{console.log('刷新操作')}}}