0

I need to create dojo button with window that pops up when user tries to leave the page.

dojo.addOnUnload(window, "test");
function test() {
  if(hasPendingChange){
    //function goes here i believe
  }
}

I am pretty sure this is the correct format but, it is leaving the page instead of waiting for user's response. Any ideas?

Thank you!

Joe Park
  • 139
  • 3
  • 12
  • 1
    You can't stop the user from leaving the page unless you use the browser's built-in dialog. See [How can I override the OnBeforeUnload dialog and replace it with my own?](http://stackoverflow.com/questions/276660/how-can-i-override-the-onbeforeunload-dialog-and-replace-it-with-my-own) – JJJ Jul 18 '12 at 16:31
  • The thing is, I would like to have my dojo warning button to show up. instead of the default warning message. Default msg only shows two option whereas I would like to add more button option to my dojo warning message. – Joe Park Jul 18 '12 at 16:41

1 Answers1

0

You cannot override the default implementation of beforeunload.

It is possible to connect an eventhandler to window.onbeforunload however, multiple handlers are a problem as the outcome may be different then what you expected.

The window.onbeforeunload is a function, called by the native JS engine and it expects a String. It will (allways) popup a native confirm(str) dialog box, that is - if there is a str returned from beforeunload function.

The only way to stop it from popping up the confirm is to make a blocking call (like dijit.Dialog{..}).show(); while(1)) and that is not really feasible, im sure you'd know that. Also, you would have to tell user, to also select 'no' in the dialog that will eventually pop when you break the endless while :)

So, example wise

var g_ChangesMade = false;
window.onbeforeunload = function() {
    return (g_ChangesMade == true ? "Leaving Page with unsaved changes" : null);
}

would present a confirmdialog with the string - if global _ Changes Are Made

  --------------------
  | Leaving Page...  |
  |                  |
  |  Ok     Cancel   |
  --------------------
mschr
  • 8,531
  • 3
  • 21
  • 35