【发布时间】:2014-07-21 14:28:26
【问题描述】:
在我的网页中,我想在用户关闭窗口时注销用户。为了捕获这个事件,我使用了 beforeunload 事件处理程序。但是当我关闭选项卡时,不会调用 removeCookie。
这里是示例代码。这是SO Question的修改代码
var preventUnloadPrompt;
var messageBeforeUnload = "my message here - Are you sure you want to leave this page?";
$('a').live('click', function () {
preventUnloadPrompt = true;
});
$('form').live('submit', function () {
preventUnloadPrompt = true;
});
if ($("#refreshFlag").val() == 1) {
preventUnloadPrompt = true;
};
$(window).bind("beforeunload", function (e) {
var rval;
if ($("#refreshFlag").val() == 1) {
preventUnloadPrompt = true;
};
if (preventUnloadPrompt) {
return;
} else {
removeCookie();
window.location = "/";
}
return rval;
})
我正在使用 JQuery 1.5.2。
我想要的是当用户刷新它应该刷新的页面并且当标签关闭时应该调用“removeCookie”。我在上面的代码中做错了什么?
UPDATE
由于刷新出现问题,此事件已从项目中删除。
【问题讨论】:
-
首先,更新到这十年的 jQuery 版本。其次,是什么让您认为您实际上可以阻止用户离开您的页面,而是重新加载页面?
标签: javascript jquery onbeforeunload