Trying to override onbeforeunload in first step of form wizard

Posted by Dirty Bird Design on Stack Overflow See other posts from Stack Overflow or by Dirty Bird Design
Published on 2010-06-18T02:41:04Z Indexed on 2010/06/18 2:43 UTC
Read the original article Hit count: 222

Filed under:

This pertains to a form wizard with 5 steps, I don't want the user to hit back and lose form data in any step 2-4. I have added a flag for the submit function and need to add this one for the first step. If they get there by accident and try and leave I dont want the cofirm dialog popping up.

<script type="text/javascript">
    $(document).ready(function(){
      var action_is_post = false;
    $("form").submit(function () {
    action_is_post = true;
 });

//this is the trouble spot. on the first step the "navigation" of the form has a class 
of current (on step one = current)
$(this).ready(function () {
if ($("#stepDesc0").is(".current")) {
action_is_post = true;    
}
);   

window.onbeforeunload = confirmExit;
function confirmExit()
{
if (!action_is_post)
return 'Using the browsers back, refresh or close button will cause you to lose all
form data. Please use the Next and Back buttons on the form.';
}
});
</script>

© Stack Overflow or respective owner

Related posts about jQuery