① js如何區分頁面是關閉還是刷新
簡單版本(沒辦法判斷多選項卡的瀏覽器):
<mce:scripttype="text/javascript"><!--
functionclose(evt)//author:sunlei
{
varisIE=document.all?true:false;
evt=evt?evt:(window.event?window.event:null);
if(isIE){//IE瀏覽器
varn=evt.screenX-window.screenLeft;
varb=n>document.documentElement.scrollWidth-20;
if(b&&evt.clientY<0||evt.altKey){
//alert("是關閉而非刷新");
window.location.href="../include/logout.php";
}
else{
//alert("是刷新而非關閉");
returnfalse;
}
}
else{//火狐瀏覽器
if(document.documentElement.scrollWidth!=0)
{
//alert("是刷新而非關閉");
//window.location.href="report_list.php?ss=1";
returnfalse;
}
else{
alert("是關閉而非刷新");
//window.location.href="repost_list.php?ss=0";
//alert("bbbbbbb");
}
}
}
//--></mce:script>
<BODYonunload="close(event);">
復雜版本(除非用任務欄關閉):
functionCloseOpen(event){
if(event.clientX<=0||event.clientY<0){
//獲取當前時間
vardate=newDate();
//將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("刷新或離開");
}
}
varcurrentKeyCode=-1;
functiondocument.onkeydown(){//本窗口的所有下屬頁面都必須含有本函數
top.currentKeyCode=event.keyCode;
}
functiononbeforeunload(){
varsw=0,s0="";
if(currentKeyCode==116)
{
s0+="刷新窗口!(F5)";
}
else
{
if((event.altKey)&&(currentKeyCode==115))
{
s0+="關閉窗口!(alt+F4)";sw=1;
//獲取當前時間
vardate=newDate();
//將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
{
//獲取當前時間
vardate=newDate();
//將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;
}
}
<bodyonunload="CloseOpen(event)"></body></html>
② 用JS判斷瀏覽器是關閉還是刷新
window.onbeforeunload = function() //author: meizz
{
var n = window.event.screenX - window.screenLeft;
var b = n > document.documentElement.scrollWidth-20;
if(b && window.event.clientY < 0 || window.event.altKey)
{
alert("是關閉而非刷新");
window.event.returnValue = ""; //這里可以放置你想版做的操作代碼權
}else
{
alert("是刷新而非關閉");
}
}
③ asp或js 判斷當前頁面是否關閉
js判斷頁面是否關閉、刷新或跳轉的方法:
window.onbeforeunload=function(){
alert("===onbeforeunload===");
if(event.clientX>document.body.clientWidth&&event.clientY<0||event.altKey){
alert("關閉了瀏覽器");
}else{
alert("正在刷新頁面");
}
}
這段代碼就是判斷觸發onbeforeunload事件時,滑鼠是否點擊了關閉按鈕,或者按了ALT+F4來關閉網頁,如果是,則認為系統是關閉網頁,否則在認為系統是刷新網頁。
④ 用JS在IE和火狐下判斷網頁是刷新還是關閉,頁面跳轉
<script type="text/javas
cript">
function close(evt) //author: sunlei
{
var isIE=document.all?true:false;
evt = evt ? evt :(window.event ? window.event : null);
if(isIE){//IE瀏覽器
var n = evt.screenX - window.screenLeft;
var b = n > document.documentElement.scrollWidth-20;
if(b && evt.clientY<0 || evt.altKey){
alert("是關閉而非刷新");
}
else{
alert("是刷新而非關閉");
}
}
else{//火狐瀏覽器
if(document.documentElement.scrollWidth!=0)
alert("是刷新而非關閉");
else
alert("是關閉而非刷新");
}
}
</script>
<body onunload="close(event);">
⑤ 如何用js監聽瀏覽器頁面的關閉/刷新事件
首先判斷瀏覽器的抄類型,簡便可用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('刷新操作');
}
};
}
⑥ js區分瀏覽器頁面是刷新還是關閉
頁面載入時只執行onload
頁面關閉時只執行onunload
頁面刷新時先執行onbeforeunload,然後onunload,最後onload。
經過驗證我得出的結論是:
//對於ie,谷歌,360:
//頁面載入時只執行onload
//頁面刷新時,刷新之前執行onbeforeunload事件,在新頁面即將替換舊頁面時onunload事件,最後onload事件。
//頁面關閉時,先onbeforeunload事件,再onunload事件。
//對於火狐:
//頁面刷新時,只執行onunload;頁面關閉時,只執行onbeforeunload事件
那麼回歸正題,到底怎樣判斷瀏覽器是關閉還是刷新?我按照網上的各種說法實驗千百遍,都未成功,其中各種說法如下:
window.onbeforeunload=function()//author:meizz
{
varn=window.event.screenX-window.screenLeft;
varb=n>document.documentElement.scrollWidth-20;
if(b&&window.event.clientY<0||window.event.altKey)
{
alert("是關閉而非刷新");
window.event.returnValue="";//這里可以放置你想做的操作代碼
}else
{
alert("是刷新而非關閉");
}
}
window.onbeforeunload=function()//author:meizz
{
varn=window.event.screenX-window.screenLeft;
varb=n>document.documentElement.scrollWidth-20;
if(b&&window.event.clientY<0||window.event.altKey)
{
alert("是關閉而非刷新");
window.event.returnValue="";//這里可以放置你想做的操作代碼
}else
{
alert("是刷新而非關閉");
}
}
⑦ js判斷頁面是關閉還是刷新
<body>標簽只有onload\onunload\onbeforeunload事件,而沒有onclose事件。不管頁面是關閉還是刷新都會執行onunload事件。如何捕捉到頁面關閉呢?
頁面載入時只執行onload
頁面關閉時只執行onunload
頁面刷新時先執行onbeforeunload,然後onunload,最後onload。這樣我們可以在onbeforeunload中加一個標記,在onunload中判斷該標記,即可達到判斷頁面是否真的關閉了。
<html>
<head>
<title>判斷頁面是關閉還是刷新</title>
</head>
<body onunload="fclose();" onload="fload();" onbeforeunload="bfunload();">
<script language="javascript">
var s = "test";
function fclose()
{
if(s=="no")
alert(』unload me!=』+s+』這是刷新頁面!』);
else
alert(』這是關閉頁面』);
}
function fload()
{
alert("load me!="+s);
}
function bfunload()
{
s = "no";
}
</script>
</body>
</html>