Home  >  Article  >  Backend Development  >  javascript - How to avoid form submission also triggering beforeunload event

javascript - How to avoid form submission also triggering beforeunload event

WBOY
WBOYOriginal
2016-08-04 09:21:311380browse

The specific requirements are as follows:
Bind the beforeunload event to the window, check whether the input content has been saved when switching or closing the page, but the beforeunload event will also be triggered when the submit button is clicked. How to prevent form submission from triggering this event?

Part of the code:

<code>    if ($scope.model.isSaving) {
      window.onbeforeunload = function (e) {
        e = e || window.event;

        // 兼容IE8和Firefox 4之前的版本
        if (e) {
          e.returnValue = '关闭提示';
        }

        // Chrome, Safari, Firefox 4+, Opera 12+ , IE 9+
        return '关闭提示';
      };
    }</code>

Just unbind it in the submit event of the button

<code>$(window).off('beforeunload')或者window.onbeforeunload = null;
</code>

Reply content:

The specific requirements are as follows:
Bind the beforeunload event to the window, check whether the input content has been saved when switching or closing the page, but the beforeunload event will also be triggered when the submit button is clicked. How to prevent form submission from triggering this event?

Part of the code:

<code>    if ($scope.model.isSaving) {
      window.onbeforeunload = function (e) {
        e = e || window.event;

        // 兼容IE8和Firefox 4之前的版本
        if (e) {
          e.returnValue = '关闭提示';
        }

        // Chrome, Safari, Firefox 4+, Opera 12+ , IE 9+
        return '关闭提示';
      };
    }</code>

Just unbind it in the submit event of the button

<code>$(window).off('beforeunload')或者window.onbeforeunload = null;
</code>

Bind button events, ajax submit form

After submitting the click event, stay on the current page and do not trigger this event! I don’t know if this works?

You can use ajax to submit the form and get the returned results.

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn