监听浏览器返回事件

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);

你可能感兴趣的:(监听浏览器返回事件)