Reputation: 1040
I am trying to get window.confirm to work with a script I am working with however it doesn't seem to wanna work . What happens is if the person presses cancel it still runs the code, And sadly now the way i wrote the code the popup box won't even show up in chrome. Not sure why though I can remove the window.confirm method and it will still show a popup box but it will just run the script regardless of which choice you make. Here is the code , Thank you for your assistance.
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();
});
Upvotes: 1
Views: 1938
Reputation: 108530
You have syntax errors in your code. Here is your code reformatted, and as you can see you are not closing your functions properly:
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();
});
Update: Updated the code don't know if there are still syntax errors or not but. Any other errors I am missing?
Upvotes: 1
Reputation: 32222
You can't stop someone from leaving the page - if they want to leave, they can. Stopping this event from propogating in the usual fashion doesn't work. The way to get a popup message to display (which will usually be surrounded by browser supplied text) is to return your string from the function fired by onbeforeunload. Try this:
function goodbye() {
if (!validNavigation)
return "Are you sure you want to leave";
else
return "";
}
window.onbeforeunload = goodbye;
Upvotes: 1