tags:

views:

30

answers:

1

Hi I have a long form that is split into a wizard. To prevent the user from hitting browser back button and losing data I have the following:

window.onbeforeunload = function() { return "Hitting back will cause you to lose all form data."; };

How would I insert a flag and apply to the last step so the form can be submitted properly? Thanks,

+1  A: 

You can set a variable on the last step to true and check against it, like this:

var okToSubmit = false;
window.onbeforeunload = function() { 
  if(!okToSubmit) return "Hitting back will cause you to lose all form data."; 
};
//set okToSubmit = true; on the last step

Without your current code I can't say exactly where to insert this, but wherever your script moves to the last step, add a okToSubmit = true; in there.

Nick Craver
worked pretty good Nick, thank you. Is there a way to make this only apply to "submit" but still pop the alert on the last step if you hit back in the browser, refresh in the browser or another page in the nav?
Dirty Bird Design
@Dirty - Sure! Just add a click handler to that submit button to set the variable, it'll execute before `window.onbeforeunload` does :) something like this: `document.getElementById('submitButtonID').onclick = function() { onToSubmit = true; };`
Nick Craver
do I still leve okToSubmmit = True; in the wizard js function?
Dirty Bird Design
@Dirty - Nope, just the submit handler...only in places you want to click and have the message/prompt disabled, wherever you set it to `true`, the message won't display again, sounds like you *only* want to do that on submit.
Nick Craver
Nevermind, I added it separately, which is wrong. it works adding it above the if(!...)i changed "{ onToSubmit = true; }; to { okToSubmit = true; } figured it was a typo. Thanks a lot Nick, this is perfect now!
Dirty Bird Design
How would you add jconfirm to this?<script type="text/javascript">var okToSubmit = false;window.onbeforeunload = function() { document.getElementById('Register').onclick = function() { okToSubmit = true; }; if(!okToSubmit) return "Using the browsers back button will cause you to lose all form data. Please use the Next and Back buttons on the form"; };</script>Im using jAlert plug in for alerts and would like to use jConfirm for the function above. When I add jConfirm after "return" it works...for a second. it pops the warning and then the page refreshes and the dialog box goes away.
Dirty Bird Design
@Dirty - You couldn't really, unless you aborted the operation, and on ok for the dialog resumed/redirected manually, but that won't handle submits...that's why SO uses the default modal as well, it's a *much* less complicated endeavor.
Nick Craver
damn browsers! thanks anyway Nick, I really appreciate your help. I can get it to pop the jConfirm dialog, but it disappears and the page refreshes OR I can get it to pop both.
Dirty Bird Design
Not sure if your still around, seem to have messed this up. Forget the jConfirm stuff, can't be done. But now its not letting me submit w/o popping confirm messge.<script type="text/javascript">var okToSubmit = false;window.onbeforeunload = function() { document.getElementById('Register').onclick = function() { okToSubmit = true; }; if(!okToSubmit) return "Using the browsers back button will cause you to lose all form data. Please use the Next and Back buttons on the form"; };</script>anything jump out at you?
Dirty Bird Design