美文网首页
监听浏览器返回事件

监听浏览器返回事件

作者: 管子先生 | 来源:发表于2018-02-01 11:10 被阅读0次
function pushHistory() {
        var state = {
            title: "title",
            url: "#"
        };
        window.history.pushState(state, "title", "#");
    }
    pushHistory();
    var bool = false;
    setTimeout(function () {
        bool = true;
    }, 1500);
    window.addEventListener("popstate", function (e) {
        if (bool) {
            if (sessionStorage.getItem('addCar')) {
                sessionStorage.removeItem('addCar');
                window.location.replace('/');
            }else{
                window.history.back();
            }
        }
        pushHistory();
    }, false);

相关文章

网友评论

      本文标题:监听浏览器返回事件

      本文链接:https://www.haomeiwen.com/subject/svfdzxtx.html