views:

524

answers:

5

I'm creating a popup window that has a beforeunload handler installed. When the "Close" file menu item is used to close the popup, the beforeunload handler is called twice, resulting in two "Are you sure you want to close this window?" messages appearing.

This is a bug with Firefox, and I've reported it here, but I still would like a way to prevent this from happening. Can you think of a sane way of detecting double beforeunload to prevent the double message problem? The problem is that Firefox doesn't tell me which button in the dialog the user elected to click - OK or cancel.

A: 

Create a global variable that is set to true inside the handler. Only show the alert/popup when this variable is false.

pygorex1
But what if the user cancels the close? Then, the next time they try to close the window, the dialog won't pop up at all.
Zarkonnen
If you're using `confirm()` for the warning message you can detect if the user canceled the close and set the global var accordingly.
pygorex1
But how would I abort the closing process at that point?
Zarkonnen
Background: if you return anything from the beforeunload handler, a dialog saying "Are you sure you want to close" is then displayed by the browser, incorporating the return value in the text. I'm not aware of any way of getting at whether the user clicked OK or cancel.
Zarkonnen
Ahh, I misunderstood the scope of the problem. It is a bug in FF. I'm not experiencing the problem with FF3.0.15 on Vista
pygorex1
A: 

Set a variable in the handler to prevent the dialog coming up the second time. Use setTimeout to reset it afterwards.

Mark Bessey
What's a sensible timeout value, though? Set it too short and slow users will end up seeing the dialog twice after all. Set it too long and fast users won't see a dialog at all - which is something that really shouldn't happen, as it could cause them to lose data.
Zarkonnen
You can probably make it really short. I assume that the two calls to beforeunload are a few milliseconds apart. Set the timeout to 100ms or so, and Boone will know it's there.
Mark Bessey
The second call will come whenever the user has pressed the "OK" button on the first dialog box, though.
Zarkonnen
A: 

This is definitely a FF bug. I've reported it at https://bugzilla.mozilla.org/show_bug.cgi?id=531199

Zarkonnen
A: 

The best solution I've found is to use a flag global variable that is reset after so many milliseconds, say 500 (this ensures that the function can be called again, but not immediately after its appearance).

See last code in:

http://social.msdn.microsoft.com/Forums/en/sharepointinfopath/thread/13000cd8-5c50-4260-a0d2-bc404764966d

artur
A: 
var onBeforeUnloadFired = false; window.onbeforeunload = function () { //if (window.clickHereDone == false) if (!onBeforeUnloadFired) { onBeforeUnloadFired = true; event.returnValue = "You have attempted to leave this page. If you have made any changes to the fields without clicking the Save button, your changes will be lost. Are you sure you want to exit this page?"; } window.setTimeout("ResetOnBeforeUnloadFired()", 10); } function ResetOnBeforeUnloadFired() { onBeforeUnloadFired = false; }
Gayathri