Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

binding popstate event not working

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

like image 731
mirelon Avatar asked Feb 07 '13 10:02

mirelon


2 Answers

Type this into the console

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

then click the back button.

like image 66
Sean Hogan Avatar answered Nov 06 '22 04:11

Sean Hogan


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

window.addEventListener('popstate', function(){alert(1);});
like image 20
Trev14 Avatar answered Nov 06 '22 06:11

Trev14