如何防止用户使用jQuery更改页面 [英] How to prevent the user to change page with jQuery
问题描述
我有一个页面,其表单是通过ajaxSubmit()提交的(因此,无需更改页面)。
I have a page with a form that is submittes via ajaxSubmit() (so, without changing the page).
我的目标是,当用户尝试时要更改页面(甚至关闭浏览器),我问他是否真的想退出页面而不发送表格(正如gmail所做的那样)。
My goal is that, when the user try to change page (or even to close the browser), i ask him if really want to exit the page without sending the form (exactly as gmail does).
Gmail for例如,使用类似window.confirm的弹出窗口,但是如果有可能,我会用自定义消息和选项来处理它。
Gmail for example do this with a window.confirm-like popup, but if it is possible, i'll like to handle it with custom messages and options.
jQuery有卸载event:
jQuery have the unload event:
$(window).unload( function () { alert("Bye now!"); } );
但它允许我在退出页面之前做一些事情;如果用户点击相对按钮,我需要阻止页面退出。
but it permits me just to do something before exit the page; i need to 'block' the page exit, if the user click the relative button.
那么,如何处理(和取消)页面退出事件?
So, how to handle (and cancel) the page-exit event?
推荐答案
尝试以下方法。演示此处
<script type="text/javascript">
function unloadPage(){
return "dont leave me this way";
}
window.onbeforeunload = unloadPage;
</script>
这篇关于如何防止用户使用jQuery更改页面的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!