1

我想检测后退按钮单击事件,所以我在 jquery 中使用 window popstate 事件。但它根本没有在任何时候开火。

请参考下面的代码

$(document).ready(function(){
            $(window).on("popstate",function(){
                debugger
            });
        });

也试过这种方式。

   window.onpopstate = function(event) {debugger
        alert("location: " + document.location + ", state: " + JSON.stringify(event.state));
    };

是否需要为此添加任何外部脚本?或其他任何需要在我的页面中配置的内容。

4

2 回答 2

1

您可以像这样使用 javascript 绑定事件侦听器:

window.addEventListener('popstate', function(event) {
    alert( "triggered" );
});
于 2014-09-12T10:32:50.337 回答
1

I believe that you need add first an history before detect back? Example:

var stateObj = { foo: "bar" };
history.pushState(stateObj, "page 2", "?foo=1");
于 2016-07-02T15:05:37.080 回答