简体   繁体   中英

window.onbeforeunload allow submit button?

I have the following javascript to show a confirm box when a user leaves the page.

My problem is, it is showing even when user clicks the submit button inside a form in my page. I don't want this to be triggered on form submit and on a span click.

How can I allow form submit and span click in the function below?

window.onbeforeunload = function (e) {
    e = e || window.event;



    return '';
};

EDIT -------------------------------

sorry, it is now a form, it is simple button and a href: I have one page that uses only a button:

<input type="button" value="Save" id="btn-crop" />

and a link:

<a href="done.php" class=button2>Save</a>

您应该能够使用e.currentTarget属性检查哪个对象调度了事件,然后在必要时显示提示。

You could call [Event].stopPropagation in an callback of submit event for each button in the page.

This function would stop the callback function of the beforeunload event when would go submit the button.

You can read more about the [Event].stopPropagation here .

Like so: Button.addEventListener("submit",function(Event){Event.stopPropagation()})

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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