Connecting Tech Pros Worldwide Help | Site Map

How to get Confirm before leaving a page

Newbie
 
Join Date: Oct 2006
Location: Dubai
Posts: 1
#1: Oct 16 '06
Hi,

I want to get confirmation before leaving a page. If the selects Yes i want to leave the page else i want to retain the page in the browser.

i tried with
Expand|Select|Wrap|Line Numbers
  1. <body  onbeforeunload="return HandleOnClose()" >
Expand|Select|Wrap|Line Numbers
  1. function HandleOnClose()
  2.  {  
  3.   if (confirm('Do you want to leave'))
  4.          return ture;
  5.   else
  6.  
  7. }
Appreciating your reply.
YenRaven's Avatar
Newbie
 
Join Date: Oct 2006
Posts: 29
#2: Oct 17 '06

re: How to get Confirm before leaving a page


Expand|Select|Wrap|Line Numbers
  1. <body onbeforeunload="return HandleOnClose()"
  2.  
  3. function HandleOnClose()
  4. {
  5. if (confirm('Do you want to leave'))
  6. return ture;
  7. else
  8. return false;
  9. }

the only probelm with this code is that most browsers now days detect for the return false onBeforeUnload and display there own error message.( a way to keep people from locking people in there website.) I would suggest doing some browser checking and just returning false for browsers with there own errors to prevent the double message.
Newbie
 
Join Date: Oct 2009
Posts: 1
#3: 4 Weeks Ago

re: How to get Confirm before leaving a page


Hi
I appreciate your soonest reply
well i want to force the user to save his work by clicking on the save button before he leaves
so i want when the popup screen appears to delete one button and leave one which says " ok " and returns him to the initial page and then when he clicks the button i know to change the trigger
2-can i change the title javascript application?
thanks
acoder's Avatar
Site Moderator
 
Join Date: Nov 2006
Location: UK
Posts: 14,581
#4: 3 Weeks Ago

re: How to get Confirm before leaving a page


Both of these are for security reasons and cannot be changed.

Don't try to force anything. Use onbeforeunload as a reminder in case they've forgotten. If they still refuse to save, well, that's another problem altogether.
Reply


Similar JavaScript / Ajax / DHTML bytes