tags:

views:

38

answers:

1

I have a basic function to alert the user upon refresh, browser back button (except the submit button) or when a link is clicked they will lose form data from a form wizard.

<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. Does anyone know how to fix this?

A: 

I believe you still need to return false;, otherwise the unload action still occurs. I'm not familiar with the plugin, but try something like:

// your code
if(!okToSubmit) 
{ 
    alert( "Using the browsers back button will cause you to lose all form data. Please use the Next and Back buttons on the form" );
    return false;
}
// the rest of your code
hookedonwinter
If the plugin expects a return of either `true` or `whatever your response is`, then disregard this answer.
hookedonwinter
thx ill give it a shot, will the "alert" work the same as a confirm dialogue?
Dirty Bird Design
nope. Alert just alerts. Confirm gives the option. So you can switch it to `var whatTheySaid = confirm( "are you srsly ready yo?" )` and then do stuff based on the return (true or false).
hookedonwinter
That solved the problem of the dialog box disappearing, although it now pops the jConfirm message as well as the standard browser message on top of it
Dirty Bird Design
I'm not sure how that plugin works. Maybe there's a way to send it the info to confirm? Or... maybe don't use the plugin?
hookedonwinter