禁用“您所做的更改可能无法保存”弹出窗口

问题描述:

我使用以下前端代码导出 .csv 文档。

I use the following frontend code to export a .csv document.

HTML

  <form id="tool-export" method="post" action="export/">{% csrf_token %}
    <a id="export-link" class="btn btn-sm btn-primary" href="#">DOWNLOAD</a>
  </form>

JS

  $('#export-link').click(function(e) {
    e.preventDefault();
    var link = $(this);
    var form = link.closest('form');

    var project_id = proj_id.find(":selected").val();
    var input = $('<input>').attr('type', 'hidden').attr('name', 'project_id').val(project_id);
    form.append($(input));

    var project_type = proj_type.val();
    input = $('<input>').attr('type', 'hidden').attr('name', 'project_type').val(project_type);
    form.append($(input));

    form.submit();
  });

导出效果很好,我得到了正确的文件。但是,在点击导出链接后,我收到 您所做的更改可能无法保存 消息。如何禁用此消息?我不想看到它。

Export works well and I get the correct document. But also I receive the Changes you made may not be saved message after clicking on the export link. How to disable this message? I don't want to see it.

@Dekel帮我搞定了。

@Dekel helped me to get it.

消息是 beforeunload 事件。
我可以用 window.onbeforeunload = null; 来禁用它。

The message is the beforeunload event. And I can disable it with window.onbeforeunload = null;.

JS

  $('#export-link').click(function(e) {
    window.onbeforeunload = null;
    e.preventDefault();
    var link = $(this);
    var form = link.closest('form');

    var project_id = proj_id.find(":selected").val();
    var input = $('<input>').attr('type', 'hidden').attr('name', 'project_id').val(project_id);
    form.append($(input));

    var project_type = proj_type.val();
    input = $('<input>').attr('type', 'hidden').attr('name', 'project_type').val(project_type);
    form.append($(input));

    form.submit();
  });