Javascript 如何捕捉事件并区分关闭和刷新事件
在我的项目中,我需要通过web使用浏览器中的close事件,服务器和数据库处理用户注销时的信息。 我不知道如何捕捉事件并区分close和refresh事件 我尝试了以下代码:Javascript 如何捕捉事件并区分关闭和刷新事件,javascript,events,Javascript,Events,在我的项目中,我需要通过web使用浏览器中的close事件,服务器和数据库处理用户注销时的信息。 我不知道如何捕捉事件并区分close和refresh事件 我尝试了以下代码: window.onbeforeunload = function() { var n = window.event.screenX - window.screenLeft; var b = n > document.documentElement.scrollWidth-20;
window.onbeforeunload = function() {
var n = window.event.screenX - window.screenLeft;
var b = n > document.documentElement.scrollWidth-20;
if(b && window.event.clientY < 0 || window.event.altKey) {
alert("close event");
}else{
alert("refresh event");
}
}
window.onbeforeunload=function(){
var n=window.event.screenX-window.screenLeft;
var b=n>document.documentElement.scrollWidth-20;
如果(b&&window.event.clientY<0 | | window.event.altKey){
警报(“关闭事件”);
}否则{
警报(“刷新事件”);
}
}
但它只捕获刷新事件。
有没有更好的办法来解决这个问题
此外,我已经阅读了,但它没有给我答案。一个想法:通过cookie判断是否登录,以获取信息 浏览器通常不会禁用cookie 如果cookie被禁用,您可以要求用户启用它 以下是cookie的一个示例:
function setCookie(name, value) //cookies setting
{
var argv = setCookie.arguments;
var argc = setCookie.arguments.length;
var expires = (argc > 2) ? argv[2] : null;
if(expires!=null)
{
var LargeExpDate = new Date ();
LargeExpDate.setTime(LargeExpDate.getTime() + (expires*1000*3600*24));
}
document.cookie = name +value }
//In Js
setCookie("a","34234523542");
//read cookie:
function WM_readCookie(name)
{
//if there is no cookie,return false;or get value and return value
if(document.cookie == '')
return false;
else
return
unescape(WM_getCookieValue(name));
}
function WM_getCookieValue(name)
{
// Declare variables.
var firstChar,lastChar;
// Get the entire cookie string.
// (This may have other
name=value pairs in it.)
var theBigCookie = document.cookie;
// Grab
just this cookie from theBigCookie string.
// Find the start of
'name'.
firstChar = theBigCookie.indexOf(name);
// If you found it,
if(firstChar != -1)
{
// skip 'name' and '='.
firstChar +=
name.length + 1;
// Find the end of the value string (i.e. the next
';').
lastChar = theBigCookie.indexOf(';', firstChar);
if(lastChar == -1) lastChar = theBigCookie.length;
// Return the
value.
return theBigCookie.substring(firstChar, lastChar);
} else
{
// If there was no cookie, return false.
return false;
}
}
一个想法:通过cookie判断是否登录,以获取信息 浏览器通常不会禁用cookie 如果cookie被禁用,您可以要求用户启用它 以下是cookie的一个示例:
function setCookie(name, value) //cookies setting
{
var argv = setCookie.arguments;
var argc = setCookie.arguments.length;
var expires = (argc > 2) ? argv[2] : null;
if(expires!=null)
{
var LargeExpDate = new Date ();
LargeExpDate.setTime(LargeExpDate.getTime() + (expires*1000*3600*24));
}
document.cookie = name +value }
//In Js
setCookie("a","34234523542");
//read cookie:
function WM_readCookie(name)
{
//if there is no cookie,return false;or get value and return value
if(document.cookie == '')
return false;
else
return
unescape(WM_getCookieValue(name));
}
function WM_getCookieValue(name)
{
// Declare variables.
var firstChar,lastChar;
// Get the entire cookie string.
// (This may have other
name=value pairs in it.)
var theBigCookie = document.cookie;
// Grab
just this cookie from theBigCookie string.
// Find the start of
'name'.
firstChar = theBigCookie.indexOf(name);
// If you found it,
if(firstChar != -1)
{
// skip 'name' and '='.
firstChar +=
name.length + 1;
// Find the end of the value string (i.e. the next
';').
lastChar = theBigCookie.indexOf(';', firstChar);
if(lastChar == -1) lastChar = theBigCookie.length;
// Return the
value.
return theBigCookie.substring(firstChar, lastChar);
} else
{
// If there was no cookie, return false.
return false;
}
}
我在试着理解你的代码。因此,当鼠标在窗口外或按下alt键时,这是一个关闭事件?我可能错了,但我希望您无法区分刷新和离开。至于你要离开的页面,在离开的时候,页面几乎不知道你是否会回来。我试图理解你的代码。因此,当鼠标在窗口外或按下alt键时,这是一个关闭事件?我可能错了,但我希望您无法区分刷新和离开。就这一页而言,无论你以哪种方式离开,你是否会回来几乎肯定是不知道的网页在离开的时候。