最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - addEventListener("popstate") not firing for .document - Stack Overflow

programmeradmin0浏览0评论

Using Ajax and trying to create a popstate event handler on the actual Ajax page using the .document object:

document.addEventListener("popstate", myPopState);

Unfortunately this does not ever seem to trigger.

My intention being that after the page is reloaded the popstate event trigger will automatically disappear.

Using Ajax and trying to create a popstate event handler on the actual Ajax page using the .document object:

document.addEventListener("popstate", myPopState);

Unfortunately this does not ever seem to trigger.

My intention being that after the page is reloaded the popstate event trigger will automatically disappear.

Share Improve this question asked Sep 15, 2016 at 16:18 NickCNickC 1,3535 gold badges16 silver badges23 bronze badges 1
  • 10 window.addEventListener("popstate", myPopState); – zer00ne Commented Sep 15, 2016 at 16:20
Add a ment  | 

1 Answer 1

Reset to default 1

inside ajax success response , you could use

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

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

UPDATE

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

});


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

alert('call your ajax here');
    return '';
});
发布评论

评论列表(0)

  1. 暂无评论