Click here to Skip to main content
15,886,644 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
I am trying to show the confirmation popup while close the tab/browser of firefox but it is not working . I am using onbeforeunload event of javascript and using the following code :


window.onbeforeunload = DetectBrowserExit;

function DetectBrowserExit() {
return 'Are you sure to exit';
}

What I have tried:

window.onbeforeunload = DetectBrowserExit;

function DetectBrowserExit() {
return 'Are you sure to exit';
}
Posted
Updated 31-Jan-17 6:15am
Comments
ZurdoDev 31-Jan-17 13:23pm    
Put a breakpoint. Is it running the code?

1 solution

Please stop trying.

The onbeforeunload is a way to clean up localStorage or cookies.
Or maybe call a server script to delete session data.

But not the block the UI from closing you're page.

Most browsers will not allow you to do this and if they do, they stop doing it in the future.

It's considered harmfull
 
Share this answer
 
Comments
Richard Deeming 31-Jan-17 14:49pm    
The onbeforeunload event is a perfectly valid and standard way of letting the user know that they're about to lose their work by navigating away from the page without saving.

Some browsers will allow the script to show a custom message; others display a standard message if any non-null value is returned from the event handler.

There is no suggestion that any major desktop browser is going to stop supporting this, and it is not considered harmful.

WindowEventHandlers.onbeforeunload | MDN[^]

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900