简体   繁体   English

当使用jQuery打开弹出窗口时,如何避免刷新页面或单击后退按钮?

[英]how to avoid refresh page or back button click when popup is open using jquery?

I am developing one stepwise form application where I am displaying some information on Modal popup. 我正在开发一个逐步表单应用程序,其中在“模态”弹出窗口上显示一些信息。 I am able to display the required information. 我能够显示所需的信息。 If I refresh the page when the Modal is open the current Modal will close. 如果在Modal打开时刷新页面,则当前Modal将关闭。 Is there any way to stop closing the Modal on page refresh? 有什么方法可以停止在页面刷新时关闭模式吗?

You can not prevent user clicking the refresh button. 您不能阻止用户单击刷新按钮。 Instead you can keep the state of the form in the local storage and in the page load event check if the user was in the middle of the form process. 相反,您可以将表单的状态保留在本地存储中,并可以在页面加载事件中检查用户是否处于表单过程的中间。

If so, just show the modal again. 如果是这样,请再次显示模态。 There's no any other way. 没有别的办法了。

 //This is an example. This should be added when the user seen the dialog. //This is just a Bootstrap example. Just to demonstrate the logic. $("#modal-dialog").on("shown.bs.modal", function(){ localStorage.setItem("IS_SET", true); }); $("#modal-dialog").on("hidden.bs.modal", function(){ localStorage.setItem("IS_SET", false); }); $(document).ready(function(){ if(localStorage.getItem("IS_SET") && localStorage.getItem("IS_SET") === true){ //Which means the user was in the form process. //And show the modal again on page refresh. $("#modal-dialog").modal("show"); } }); 

If you don't like the localstorage method, you can set a URL param which can be seen publicly in the URL. 如果您不喜欢localstorage方法,则可以设置一个URL参数,该参数可以在URL中公开看到。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM