addEventListener("popstate") 不为 .document 触发

Nic*_*ckC 6 javascript ajax popstate

使用 Ajax 并尝试使用 .document 对象在实际的 Ajax 页面上创建 popstate 事件处理程序:

document.addEventListener("popstate", myPopState);
Run Code Online (Sandbox Code Playgroud)

不幸的是,这似乎永远不会触发。

我的意图是在页面重新加载后,popstate 事件触发器将自动消失。

man*_*tam 1

在ajax成功响应中,您可以使用

         var msg="Any thing which u want";
         var customUrl ="www.stackoverflow.com";

 window.history.pushState({"html":msg,"pageTitle":"My Title"},"", customUrl);
       window.onpopstate = function(event) {
             alert('back is clicked');
           // what ever u want
       }
Run Code Online (Sandbox Code Playgroud)

更新

$(window).unload(function(e){
    e.preventDefault();
    $(window).trigger('beforeunload');   

});


$(window).bind('beforeunload',function(){

alert('call your ajax here');
    return '';
});
Run Code Online (Sandbox Code Playgroud)