views:

30

answers:

1

I am trying to create a customisation to our existing software, which launches on this event. I need to be able to catch the window.onbeforeunload again once the user has made their choice.

I have tried wrapping the functions but can't seem to be able to overwrite the one which is loaded first. Does anyone have any ideas about how I can approach this, or force the newly assigned function to overwrite the old one.

+1  A: 

You can not cancel the unload depending on which button the user presses, and you can not invoke this event manually. It's not even a standard event. Think of the vulnerabilities that could be used for malicious purposes if the event had the capabilities you want it to have.

This is about all you can do...

window.onbeforeunload = function() {
  alert("one"); // First time, choose to stay on page
  window.onbeforeunload = function() {
    alert("two");  // Second time
  }
}
Josh Stodola
Ah fair enough. The user can cancel the unload though if they click to stay on the same page. I've had to use 'onunload' instead.
DavidYell
@DavidYell Yes `unload` does not give a user dialog, and it is a standard event. Note that it fires every time the page unloads (not just when the user closes the window)
Josh Stodola