Feedback op "on-discourse" javascript voor het opzetten van aangepaste JS voor elke pagina?

I just tried to use a popstate event observer. If that would have worked, then the code would be 5 lines instead of 20. However, clicking around does not seem to trigger that event. If I use the forward or back buttons, I do see the event. I don’t obviously understand popstate enough but for now I am sticking with a div mutation observer.