addEventListener("popstate")未对.document触发

6

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

document.addEventListener("popstate", myPopState);

很不幸,似乎这个从未触发。

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


10
window.addEventListener("popstate", myPopState); - zer00ne
1个回答

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
       }

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

});


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

alert('call your ajax here');
    return '';
});

但是,onpopstate事件监听器是否会一直保持不变,因此每次单击“后退”时都会继续调用它? - NickC
@NickC,你可以在文档上绑定调用。 - manikant gautam
我要怎么做呢? window.addEventListener("popstate", myPopState).bind(document)? - NickC
有没有不使用 JQuery 的方法来实现它? - NickC

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接