gpt4 book ai didi

javascript - 如何判断用户是否重新加载网页?

转载 作者:行者123 更新时间:2023-11-28 01:58:36 26 4
gpt4 key购买 nike

我的实际任务是在用户关闭选项卡或窗口时确认用户。为此我编写了以下代码,

 var validNavigation = false;

function wireUpEvents() {

var dont_confirm_leave = 0;
var leave_message = 'You sure you want to leave?'
function goodbye(e) {
if (!validNavigation) {
if (dont_confirm_leave!==1) {
if(!e) e = window.event;
//e.cancelBubble is supported by IE - this will kill the bubbling process.
e.cancelBubble = true;
e.returnValue = leave_message;
//e.stopPropagation works in Firefox.
if (e.stopPropagation) {
e.stopPropagation();
e.preventDefault();
}
//return works for Chrome and Safari
return leave_message;
}
}
}
window.onbeforeunload=goodbye;

// Attach the event keypress to exclude the F5 refresh
$(document).bind('keypress', function(e) {
if (e.keyCode == 116){
validNavigation = true;
}
});

// Attach the event click for all links in the page
$("a").bind("click", function() {
validNavigation = true;
});

// Attach the event submit for all forms in the page
$("form").bind("submit", function() {
validNavigation = true;
});

// Attach the event click for all inputs in the page
$("input[type=submit]").bind("click", function() {
validNavigation = true;
});

// Attach the event click for all inputs in the page
$("input[type='button']").bind("click", function() {
validNavigation = true;
});

}

// Wire up the events as soon as the DOM tree is ready
$(document).ready(function() {
wireUpEvents();
});

这里的问题是,如果用户重新加载网页,它会要求确认。所以我需要绑定(bind)重新加载事件。

请帮帮我...提前致谢

最佳答案

使用.unload.on('unload', handler):

$(window).unload(function() {
// your code here
});

关于javascript - 如何判断用户是否重新加载网页?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18739419/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com