我正在尝试让 window.confirm 与我正在使用的脚本一起工作,但它似乎并不想工作。会发生什么情况是,如果该人按下取消,它仍然会运行代码,可悲的是现在我编写代码的方式弹出框甚至不会显示在 chrome 中。不知道为什么我可以删除 window.confirm 方法,它仍然会显示一个弹出框,但无论您做出哪种选择,它都会运行脚本。这是代码,感谢您的帮助。
var validNavigation = false;
function wireUpEvents() {
var dont_confirm_leave = 0; to be able to leave withou confirmation
var leave_message = 'Leaving the page will terminate your Self-Service or Kiosk session.';
function goodbye(e) {
if (!validNavigation)
function goodbye() = {
var x = window.confirm("Leaving the page will terminate your Self-Service or Kiosk session.")
if (x)
{
window.onunload=leave;
}
else {
return "" ;
}
}}
function leave() {
if (!validNavigation) {
killSession();
}
}
//set event handlers for the onbeforeunload and onunloan events
window.onbeforeunload=goodbye;
//window.onunload=leave;
}
// Wire up the events as soon as the DOM tree is ready
jQuery(document).ready(function() {
wireUpEvents();
});
最佳答案
您无法阻止某人离开页面 - 如果他们想离开,他们可以离开。阻止此事件以通常的方式传播是行不通的。获取要显示的弹出消息(通常被浏览器提供的文本包围)的方法是从 onbeforeunload 触发的函数返回您的字符串。试试这个:
function goodbye() {
if (!validNavigation)
return "Are you sure you want to leave";
else
return "";
}
window.onbeforeunload = goodbye;
关于javascript - 让 window.confirm 工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12566782/