'Leave this page' message display twice on

2019-08-24 18:54发布

I have these line of code using onbeforeunload to ask user to save their data before closing the page. The code works well in Firefox & Chrome.

window.onbeforeunload = function () {
    if (isUpdated) return "You have unsaved data";
    else return undefined;
};

enter image description here

In IE (10), when I chose "Leave this page", everything's fine. But when I chose "Stay on this page", the popup closed and then display again, right after that, I have to click one more time on "Stay on this page", which I find annoying.

Anyone have a solution for this?

1条回答
唯我独甜
2楼-- · 2019-08-24 19:04

Add the onbeforeunload event handler to window.href instead of window:

window.href.onbeforeunload = function () {
    if (isUpdated) {
        return "You have unsaved data";
    } else {
        return undefined;
    }
};
查看更多
登录 后发表回答