gpt4 book ai didi

javascript - addEventListener ("popstate") 不触发 .document

转载 作者:行者123 更新时间:2023-11-29 15:25:36 26 4
gpt4 key购买 nike

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

document.addEventListener("popstate", myPopState);

不幸的是,这似乎从未触发过。

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

最佳答案

在 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 '';
});

关于javascript - addEventListener ("popstate") 不触发 .document,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39515893/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com