开发者

jConfirm and onbeforeunload

开发者 https://www.devze.com 2023-01-03 08:27 出处:网络
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.

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?


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
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号