6

In IE and FF, i can attach an event handler to onBeforeUnload, and by passing a string to a property of the event, the user will see a dialog asking him whether he wants to continue with the "unloading" (either closing the window or navigating away).

Safari and Chrome don't support onBeforeUnload, and onUnload seems to be too late. Is there some way to replicate the same functionality in Safari/Chrome?

NOTE: I'm not trying to keep the user hostage. I know this is nagging and un-cool. In fact, my site goes to great lengths to let the user go freely, and have everything in its place when they come back. However, I am hosting other sites inside IFrames, and sometimes these decide to get rid of me and take over the browser, which is what I'm trying to avoid.

Thanks!

Daniel Magliola
  • 30,898
  • 61
  • 164
  • 243
  • Note that it's generally considered rude to display other people's sites in your own frames, and there was a huge outcry when Digg recently did just that. – Brian Campbell Apr 29 '09 at 20:12
  • I know, and thanks for the suggestion. However, we do have a VERY different target than Digg, and (we believe at least) we have a good reason to do this, it does provide some real functionality. – Daniel Magliola Apr 29 '09 at 20:36

4 Answers4

7

This works perfectly for me in both Chrome and Safari:

<html><body><p>Test</p>
<script>window.onbeforeunload = function() { return "Sure?"; }</script>
</body></html>

When I try to close the window, I get the prompt.

RichieHindle
  • 272,464
  • 47
  • 358
  • 399
  • Hmmmmmmm. That's weird... My code wasn't working in Safari. Maybe it's because I was using Prototype's Event.observe() to attach the handler... Anyways, thanks a lot! You just made my life so much easier! – Daniel Magliola Apr 29 '09 at 20:35
  • @RichieHindle Can I call the parent window to refresh when closing the child using this method? or can I let this call another js function instead of confirmation ? – raym0nd Apr 30 '12 at 20:49
  • @raym0nd: The `onbeforeunload` handler has a bunch of restrictions on what it can do. I'm not exactly sure what thay are, and they're likely to vary between browsers. You should test it, but I would be doubtful that it would work everywhere, now and in the future. – RichieHindle May 01 '12 at 08:50
  • 2
    The key part of this which I missed initially is returning a string value. In Chrome simply 'function() { alert('Sure?'); }' will not work. – James Hulse Sep 25 '12 at 04:11
2

I checked it in chrome and it seems to be working fine using this:

<body onunload="alert('x');" onbeforeunload="test1();">
Ogie
  • 21
  • 2
2

StackOverflow itself uses onbeforeunload, and it works fine for me in Safari:

function setConfirmUnload(a){window.onbeforeunload=a?function(){return a}:null}
Brian Campbell
  • 322,767
  • 57
  • 360
  • 340
1

This question is covered in slightly more detail in another newer StackOverFlow question: Setting onbeforeunload on body element in Chrome and IE using jQuery

Community
  • 1
  • 1
AnthonyVO
  • 3,821
  • 1
  • 36
  • 41