简体   繁体   中英

How to have window message stay and not leave the page until user clicks button?

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!

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) 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   |
  --------------------

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM