Multiple onpopstate event
Asked Answered
C

2

5

I need to work with History API for an application. My problem is that some "home" jQuery plugins already implement an event for popstate

it is possible to have more than one popstate event for an unique view ?

Christine answered 4/1, 2017 at 11:49 Comment(0)
D
14
window.addEventListener("popstate", console.log);
window.addEventListener("popstate", function(){console.log("I am also called")});

This seems to work fine for me.

Edit: Now with multiple events. For some reason Chrome won't fire twice the console log of the event though

Diapedesis answered 4/1, 2017 at 11:52 Comment(3)
It work too for me, my question is if is possible to have two event like that for the same pageChristine
Axnyff console.log is a function by itself. I don't see the reason calling it on an anonymous function. Just call "console.log("Message")" again. Works fine for me! And yes OrcusZ, you can use it more than one times.Housemother
Thanks this work. Another problem that they are all fired at the same time and i want to manage them separatlyChristine
E
1

I mistakenly used the following in two React components (Result: The second component overrides the behaviour of the first component, so it didn't trigger):

window.onpopstate = function (e) { /* Do something */ }

Instead I used the following:

window.addEventListener("popstate", /* Do something */ )
Echikson answered 2/3, 2023 at 13:44 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.