简体   繁体   English

javascript - 防止加载页面

[英]javascript - Prevent page loading

I want to prevent the page loading when "Editing" on m page occures. 我希望在m页面上“编辑”时阻止页面加载。

so i got this code 所以我得到了这个代码

window.onbeforeunload = function() {
  return "Are you sure you want to navigate away?";
}

Now i need to unbind this from the page. 现在我需要从页面解除绑定。

is there any methods available ? 有什么方法可用吗?

Thank you. 谢谢。

您将处理程序设置为null:

window.onbeforeunload = null;

To unbind the event, set it to null: 要取消绑定事件,请将其设置为null:

window.onbeforeunload = null;

It may however be better for program flow to put a condition inside your onbeforeunload handler, so that you have a single function running on page unload: 但是,程序流可能会更好地在onbeforeunload处理程序中放置一个条件,以便在页面卸载时运行单个函数:

window.onbeforeunload = function() {
    if (myVar == "Editing") 
        return "Are you sure you want to navigate away?";
}

You should use some flag to check if page is editing values or just navigating to other page 您应该使用一些标志来检查页面是否正在编辑值或只是导航到其他页面

you can follow this thread too Javascript, controlling an onbeforeunload tag 你也可以按照这个线程Javascript,控制onbeforeunload标签

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

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