View Full Version : Prevent close event of browser or browser tab using javascript

02-22-2012, 10:56 AM
Hi All,

I have a requirement of preventing user from closing browser tab when user click on close button of tab.

When a user click on close button of a tab at that time confirmation box should be displayed that prompt user that whether to close or not?

I know it is coming by default of browser but I want my own message to display so I used confirm box. But when users closes then tab is closed.

I just want to know that if user click on cancel then what should I write to keep that tab open?

Please Help


02-22-2012, 05:38 PM
Perhaps not all browsers will work with this. But Firefox and IE do:

onbeforeunload = function(e){
if(confirm('Are you nuts? Do you really want to leave me?')){
return 'OK, Good Bye then';
else {
e = e || event;
e.returnValue = false;
return 'I said, "Are you nuts? Do you really want to leave me?"';