Confirmation on Leaving the Current Page in JavaScript

There are some places when you have to ask the user for confirmation when they leave the current page. This article shows you have to implement it.

I noticed a new feature in Gmail - the browser will ask the user to confirm exit if the user leaves in the middle on an operation. If you want to see this in action, just delete an email and close the window quickly. If you are fast enough, you will see this message...

You can use this code to implement this feature on your page.

function goodbye(e) {
	if(!e) e = window.event;
	//e.cancelBubble is supported by IE - this will kill the bubbling process.
	e.cancelBubble = true;
	e.returnValue = 'You sure you want to leave?'; //This is displayed on the dialog

	//e.stopPropagation works in Firefox.
	if (e.stopPropagation) {
		e.stopPropagation();
		e.preventDefault();
	}
}
window.onbeforeunload=goodbye;

This code basically cancels the page exit action. The browser intercepts the attempt to cancel the page exit. I don't think this is in the JavaScript specifications - but its still a good thing. Otherwise, spammers can make it very hard for people to leave their page. Another reason why its a good thing is because its implemented in both IE and Firefox in a very similar way. So you don't need any coding gymnastics to use this feature.

Comments

Danesh Uthuranga at 31 Mar, 2008 01:08
Thank you very much for this service.
I was searching for this code..

Great work

Dan
Reply to this.
Anonymous at 15 May, 2008 09:11
Thank you so much for posting this code.

Your a life saver, I've been searching for similar code for ages,

Super work!!

Ang
Reply to this.
Anonymous at 28 May, 2008 03:03
I spent several hours today looking for this capability. However, I've found it does not work with Safari (Mac v3.1.1). Any suggestions in that area? I know it's possible, since I've seen a site that uses this successfully with Safari.
Reply to this.
mrtoner at 29 May, 2008 04:04
Spending a few more hours yielded this solution:

http://www.boutell.com/newfaq/creating/disableclose.html

This seems to work with IE,FF, and Safari.
Reply to this.
chris at 26 Dec, 2008 10:31
excellent code snippet it's appreciated :)
Reply to this.
Amit at 30 Dec, 2008 06:18
Is it possible to localize the complete confirmation message as top of message "Are you..." and bottom line "Press OK..." is coming from browser.



Whatever I have learned it is not possible to completly cutomize the confirmation message is it correct or can we change it..

Thanks for all your help
Reply to this.
mrtoner at 20 Feb, 2009 02:54
That's correct: you cannot change the strings at the top and bottom of the message. Those are coming straight from the browser, not the Javascript.
Reply to this.
Anonymous at 10 Apr, 2009 06:12
I get error: e not defined. Whats the variable we're supposed to be passing??
Reply to this.
Anonymous at 14 May, 2009 01:26
thank you
Reply to this.
Anonymous at 18 Sep, 2009 11:41
wow grt stuff ...

you keep rocking ..

superb

damn good
Reply to this.
Comment

Please dont enter you comments in this form - this is a fake form to confuse spamming bots. The next form is the real one.




Comment




Comment Formating : HTML tags a, strong, em, b, i, code, pre, p and br allowed. Other tags will be shown as code(< will become &lt;). Urls, Line breaks will be auto-formated.