2

This is the problem: my web application (php) has a wizard feature which gathers customer's data page by page, and stores it in session. If customer tries to navigate away from the page before they have completed the wizard, I would like to display a massage to the effect of "You will lose your data". If the customer chooses to navigate away, the session data should be wiped.

I know that I can intercept this action by binding onpageunload event, but is there a way to then make another call, e.g. ajaxClearWizard() if the customer says "yes"?

PS I can see that, perhaps session shouldn't be used here, but I'm using an existing library, and although this wizard-data-persistence used to be a required feature the business now requires it to be removed :(

Any ideas, alternatives? Thanks in advance!

mplungjan
  • 169,008
  • 28
  • 173
  • 236
Katya S
  • 1,321
  • 3
  • 17
  • 31
  • There are some reasonable answers to [this question](http://stackoverflow.com/questions/3939961/on-unload-event-of-browser). Upshot is: you can't do that. – RichardTowers Dec 28 '12 at 11:51

1 Answers1

3
window.onbeforeunload = function () {
  return 'Your content has not been properly saved yet!';
};

This will make the browser display a confirmation box in middle with above content.

Milind Anantwar
  • 81,290
  • 25
  • 94
  • 125
  • This is not where the problem lies - using your answer, customer will see a message box, click "yes" and navigate away and the session data will still be there. What I want to happen is, if the customer clicks "yes" I want to call my own method "ajaxClearWizard" – Katya S Dec 28 '12 at 11:59