窗口popstate事件不触发在Firefox和铬

问题描述:

我想检测backbutton点击事件,所以我使用jquery中的窗口popstate事件。但它在任何时候都不会开火。窗口popstate事件不触发在Firefox和铬

请参考下面的代码

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

也尝试过这种方式。

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

是否有任何外部脚本需要添加?或其他任何需要在我的页面中配置的信息。

您可以将事件侦听器绑定,像这样的javascript:

window.addEventListener('popstate', function(event) { 
    alert("triggered"); 
}); 
+0

Thanks.it原产javscript为什么jQuery的一个不触发 – SivaRajini 2014-09-12 10:34:06

+0

没有它不是在所有的发射 – SivaRajini 2014-09-12 10:35:34

+0

你得到任何错误?它为我工作。 – TheFrozenOne 2014-09-12 10:37:01

我相信你需要添加的第一个前史检测回来吗? 例子:

var stateObj = { foo: "bar" }; 
history.pushState(stateObj, "page 2", "?foo=1");