window.onpopstate不起作用任何警报或控制台错误

时间:2019-01-24 08:38:35

标签: javascript jquery ajax

我对这个问题有疑问 How to call ajax again when user click back button to go back last webpage?

我已逐步进行了操作,但无法正常工作。

此功能无效

window.onpopstate = function (e) {
    var res = localStorage.getItem('response');         
    success(res);
}

我尝试至少显示console.log(e),但控制台中什么都没有显示

1 个答案:

答案 0 :(得分:0)

这里有人可以向我解释它应该如何工作吗? 我有相同的代码

$.ajax({
        url : ajax_url,
        type : 'POST',
        data : {
            sorting : sorting,
            action : 'filterSorting'
        },
        async: true,
        cache: false,
        error : function (responce) {
            console.log(responce);
        },
        success : function (responce) {
            // window.history.pushState({}, '');
            window.localStorage.setItem("response", responce);
            $('#products .row-products').html(responce);


            setTimeout(function() {
                $('.loading').addClass('hide');
                $('.loading').delay(2000).remove();
            }, 1000);
        }
    });
    return false;

    window.onpopstate = function (e) {
    var responce = window.localStorage.getItem('response');
    $('#products .row-products').html(responce);
}

window.onpopstate仅在我在浏览器中单击两次时才起作用。请告诉mi为什么第一次单击后无法使用