confirm page exit

JavaScript and client side scripting.

Moderator: General Moderators

Post Reply
GeXus
Forum Regular
Posts: 631
Joined: Sat Mar 11, 2006 8:59 am

confirm page exit

Post 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;
    
 
   }
 }
);
 
User avatar
arjan.top
Forum Contributor
Posts: 305
Joined: Sun Oct 14, 2007 4:36 am
Location: Hoče, Slovenia

Re: confirm page exit

Post by arjan.top »

That's why there is onbeforeunload event, "bad" thing is that not all browsers support it
User avatar
s.dot
Tranquility In Moderation
Posts: 5001
Joined: Sun Feb 06, 2005 7:18 pm
Location: Indiana

Re: confirm page exit

Post 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.
Set Search Time - A google chrome extension. When you search only results from the past year (or set time period) are displayed. Helps tremendously when using new technologies to avoid outdated results.
Post Reply