Page 1 of 1

confirm page exit

Posted: Mon Feb 11, 2008 12:35 am
by GeXus
I'm trying to display a confirm if a user tries to exit a page... This is the code I have (using jquery)... The problem is the return false, does nothing... it still exists or refreshes, or whatever.. Anyone know how I can prevent the page from closing, redirecting, refreshing, etc if a user clicks "Cancel", thanks!

Code: Select all

 
$(window).unload(function(){
    
 
   exit = confirm("Your settings are not saved, are you sure you want to leave this page?");
   if(exit == false){
    
     return false;
    
 
   }
 }
);
 

Re: confirm page exit

Posted: Mon Feb 11, 2008 4:13 am
by arjan.top
That's why there is onbeforeunload event, "bad" thing is that not all browsers support it

Re: confirm page exit

Posted: Mon Feb 11, 2008 4:21 am
by s.dot
To shorten your code a little bit you could just..

Code: Select all

return confirm('...');
That will return true/false.

And yeah, onbeforeunload() is what you're looking for.