Questions tagged [onbeforeunload]

The onbeforeunload event is triggered before a user navigates away. This event listener can be used to show a confirmation dialog to the user on page leave.

The onbeforeunload event is triggered before a user navigates away. This event listener can be used to show a confirmation dialog to the user on page leave.

In most browsers, the return value of the onbeforeunload event is displayed in the dialog, together with a default message. Since Firefox 4, this value is not displayed any more.

Example

window.onbeforeunload = function(e) {
    return 'Dialog text here.';
};

See also:

908 questions
387
votes
12 answers

How can I override the OnBeforeUnload dialog and replace it with my own?

I need to warn users about unsaved changes before they leave a page (a pretty common problem). window.onbeforeunload = handler This works but it raises a default dialog with an irritating standard message that wraps my own text. I need to either…
flesh
  • 23,725
  • 24
  • 80
  • 97
313
votes
17 answers

How to show the "Are you sure you want to navigate away from this page?" when changes committed?

Here in stackoverflow, if you started to make changes then you attempt to navigate away from the page, a javascript confirm button shows up and asks: "Are you sure you want to navigate away from this page?" blee blah bloo... Has anyone implemented…
Shimmy Weitzhandler
  • 101,809
  • 122
  • 424
  • 632
175
votes
10 answers

Prevent a webpage from navigating away using JavaScript

How to prevent a webpage from navigating away using JavaScript?
Software Enthusiastic
  • 25,147
  • 16
  • 58
  • 68
92
votes
8 answers

window.onbeforeunload not working on the iPad?

Does anyone know if the onbeforeunload event is supported on the iPad and/or if there's a different way to use it? I've tried pretty much everything, and it seems like the onbeforeunload event is never triggered on the iPad (Safari…
Art Zambrano
  • 929
  • 1
  • 7
  • 4
64
votes
6 answers

jQuery UI Dialog OnBeforeUnload

I have a small problem. I'm attempting to catch the OnUnLoad Event of the Window and ask a confirmation question and if the user decides they want to stay then fine, and if they want to leave the page then they'll lose all unsaved data. Here's the…
clockwiseq
  • 4,189
  • 9
  • 38
  • 61
60
votes
4 answers

How to prevent closing browser window?

I tried the following code to get an alert upon closing a browser window: window.onbeforeunload = confirmExit; function confirmExit() { return "You have attempted to leave this page. If you have made any changes to the fields without clicking the…
user42348
  • 4,181
  • 28
  • 69
  • 98
48
votes
4 answers

Is there any way to use window.onbeforeunload on Mobile Safari for iOS devices?

Looks like Apple has disabled the window.onbeforeunload event for iOS devices (iPhone, iPad, iPod Touch). Unfortunately I can't find any documentation as to why this event doesn't work in Mobile Safari. Does anyone know if there's a reliable…
8three
  • 661
  • 1
  • 5
  • 10
44
votes
4 answers

Difference between onbeforeunload and onunload

What are the differences between onbeforeunload and onunload ? Also I have a specific question related to it's use on the iPad...I have a page (myPage.html) where I am trying to show an alert when the page is closed (i.e. X is pressed to close the…
copenndthagen
  • 49,230
  • 102
  • 290
  • 442
40
votes
2 answers

Is there an alternative method to use onbeforeunload in mobile safari?

Is there an alternative method to use instead of onbeforeunload in mobile-safari? I've noticed that Google was able to capture the onbeforeunload event in mobile-safari. Has anyone figured out how they are doing so? Google was able to do so using…
RJ Herbert
  • 401
  • 1
  • 4
  • 5
39
votes
5 answers

Crossbrowser onbeforeunload?

Does window.onbeforeunload() fire in all browsers? I need a onbeforeunload functionality which is supported at least by IE6 and FF3.6. For IE, onbeforeunload() seems only to be supported by IE9
powtac
  • 40,542
  • 28
  • 115
  • 170
39
votes
4 answers

window.onbeforeunload - Only show warning when not submitting form

I am using window.onbeforeunload to prevent the user from navigating away after changing values on a form. This is working fine, except it also shows the warning when the user submits the form (not desired). How can I do this without showing the…
mtmurdock
  • 12,756
  • 21
  • 65
  • 108
36
votes
6 answers

Way to know if user clicked Cancel on a Javascript onbeforeunload Dialog?

I am popping up a dialog box when someone tries to navigate away from a particular page without having saved their work. I use Javascript's onbeforeunload event, works great. Now I want to run some Javascript code when the user clicks "Cancel" on…
at.
  • 50,922
  • 104
  • 292
  • 461
35
votes
3 answers

jQuery beforeunload custom pop up window for leaving a page

Hi I would like to customize my pop up for leaving a page, is there any simple way to do that? I am using simple jQuery $(document).ready(function() { var myPopUp = $('#pop-up').css('display', 'block'); $(window).bind('beforeunload',…
dikei
  • 439
  • 1
  • 6
  • 12
33
votes
7 answers

How to disable beforeunload action when user is submitting a form?

I have this little piece of code: I wonder, how could I disable this request when user hits the submit…
pawelmysior
  • 3,190
  • 3
  • 28
  • 37
31
votes
6 answers

Using onbeforeunload without dialog?

I'm trying to post data when a user leaves my page. I have finally managed to find a working solution, however, it shows a confirmation dialog when the user leaves. I have tried return null; but it didn't work. Is it possible to disable the…
user198989
  • 4,574
  • 19
  • 66
  • 95
1
2 3
60 61