jConfirm and onbeforeunload

708 Views Asked by At

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?

1

There are 1 best solutions below

5
On

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