【问题标题】:Alert not showing on browser close after doing reload of the browser, same issue is there in stackoverflow website also重新加载浏览器后关闭浏览器时不显示警报,stackoverflow 网站中也存在同样的问题
【发布时间】:2017-08-31 10:10:30
【问题描述】:

我必须在浏览器关闭并刷新特定页面时显示警报。下面的代码适用于所有场景,但在下面的场景中失败。 1.首先进行刷新,然后警报会出现并要求重新加载,现在进行重新加载。 2.现在点击浏览器关闭按钮 问题:- 现在警报没有出现在 chrome 和 Mozilla 浏览器中,在 Edge 中刷新时,警报多次出现我的代码是:-

window.onbeforeunload = (ev) => {
    let msg;
    debugger;
    if (sessionStorage.getItem('previousPage') === 'employee') {
       return 'Are you sure you want to navigate away?If you navigate away, your information will not be saved.'
        ev.preventDefault();
    }
    return msg;
};

请帮助我如何处理这种情况?

【问题讨论】:

    标签: javascript reactjs browser onbeforeunload


    【解决方案1】:

    试试这个,和这个很像:Clear localStorage on tab/browser close but not on refresh:

    window.onbeforeunload = function (ev) {
        window.onunload = function () {
            let msg;
            debugger;
            if (sessionStorage.getItem('previousPage') === 'employee') {
               return 'Are you sure you want to navigate away?If you navigate away, your information will not be saved.'
                ev.preventDefault();
            }
            return msg;
        }
    };
    

    【讨论】:

    • 以上代码不起作用,现在它不会显示警报。返回消息应该在 onbeforeunload 中。
    猜你喜欢
    • 2015-04-06
    • 2022-01-04
    • 1970-01-01
    • 2023-04-09
    • 1970-01-01
    • 2018-09-04
    • 1970-01-01
    • 1970-01-01
    • 2011-09-13
    相关资源
    最近更新 更多