binding popstate event not working

十年热恋 提交于 2019-12-07 01:34:00

问题


I have tried to type this code into the browser's console:

window.onpopstate = function() {alert(1);}

and then click the back button. No alert has shown. Am I doing something wrong? Or is it not allowed to bind popstate event to a page from console?

Using Chrome 24 and Firefox 18


回答1:


Type this into the console

window.onpopstate = function() {alert(1);}; history.pushState({}, '');

then click the back button.




回答2:


I prefer adding the popstate listener as follows, to prevent overwriting of what's already in window.onpopstate:

window.addEventListener('popstate', function(){alert(1);});


来源:https://stackoverflow.com/questions/14748452/binding-popstate-event-not-working

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!