我正在尝试使用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();
});
答案 0 :(得分:1)
您的代码中存在语法错误。这是您的代码重新格式化,正如您所看到的,您没有正确关闭您的功能:
var validNavigation = false;
function wireUpEvents() {
var dont_confirm_leave = 0;
var leave_message = document.getElementById("kioskform:broswerCloseSubmit");
var leave_safari = document.getElementById("kioskform:broswerCloseSafari");
function goodbye(e) {
if (!validNavigation) {
function disp_confirm() {
var leaveMessage = confirm("Are you sure you want to leave")
if (leaveMessage == true) {
if (dont_confirm_leave !== 1) {
if (!e) e = window.event;
//for IE
e.cancelBubble = true;
e.returnValue = leave_message.click();
//e.stopPropagation works in Firefox.
if (e.stopPropagation) {
e.stopPropagation();
e.preventDefault();
}
//return works for Chrome and Safari
leave_safari.click();
return '';
//add the code to delete the kiosk information here.
// this is what is to be done.
}
} else {
alert("Returning to the page.")
}
}}
window.onbeforeunload = goodbye;
// Attach the event keypress to exclude the F5 refresh
jQuery('document').bind('keypress', function(e) {
if (e.keyCode == 116) {
validNavigation = true;
}
});
// Attach the event click for all links in the page
jQuery("a").bind("click", function() {
validNavigation = true;
});
// Attach the event submit for all forms in the page
jQuery("form").bind("submit", function() {
validNavigation = true;
});
// Attach the event click for all inputs in the page
jQuery("input[type=submit]").bind("click", function() {
validNavigation = true;
});
}
// Wire up the events as soon as the DOM tree is ready
jQuery(document).ready(function() {
wireUpEvents();
});
更新:更新了代码,不知道是否还有语法错误。我遗失的任何其他错误?
答案 1 :(得分:1)
你无法阻止某人离开页面 - 如果他们想要离开,他们可以。阻止这个事件以通常的方式传播是行不通的。获取弹出消息的方式(通常由浏览器提供的文本包围)是从onbeforeunload触发的函数返回字符串。试试这个:
function goodbye() {
if (!validNavigation)
return "Are you sure you want to leave";
else
return "";
}
window.onbeforeunload = goodbye;