Need help getting function to fire when navigating forums

I’ve been teaching myself JS by making things for the forums and getting my functions to fire when I needed to was always a problem. I thought I had figured it out, however it seems that what I had has become inconsistent. It might always have been inconsistent and I just didn’t notice, or it might be due to a change on Discourse, but about 1/5 of the time it just doesn’t fire, and I can’t figure out why.

What I had was this

function setTriggers() {
		//when loading the window
		window.addEventListener('load', initialiseScript);

		//when using back and forth buttons
		window.addEventListener('popstate', initialiseScript);

		//when navigating
				var pushState = history.pushState;
				history.pushState = function(state) {
						if (typeof history.onpushstate == "function") {
								history.onpushstate({state: state});
						return pushState.apply(history, arguments);

I believe both the regular event listeners work fine, the one causing me problem is the bottom one, which handles navigation between threads. To be honest I don’t entirely understand how it works, it’s something I found months ago when searching for how to solve the navigation problem.

If anyone knows why it’s inconsistent or if you have something else I could use to detect navigation that would be amazing. The only alternative I have myself is setting an interval to check the URL every second to see if it’s updated, and I’d rather not do that.

I tried to get that script to keep going whenever any sort of navigation occurs. But it sometimes, seemingly completely at random, it just decides it’s had enough and won’t fire anymore until an F5. So, probably not a great example, but it’s something… I guess?

1 Like

Just tired your code and it has the exact same problem. About 1/5 of the times it just doesn’t fire when navigating between threads. Thanks anyway, though. :hugs:

Calling history.onpushstate directly seems wrong. I assume that’s included in the behavior of the original method, which you are calling via pushState.apply. If my guess is correct, that means onpushstate would be getting called twice. Try removing the following and see if it works

if (typeof history.onpushstate == "function") {
	history.onpushstate({state: state});
1 Like

It doesn’t seem to make any difference, so I suppose you’re right in that it was superfluous, but the issue persists.

edit: I have been mistaken, maybe this does work. Let me try some more

edit2: Yeah, that seems to have done it. Thanks! I wonder why it was there to begin with then.


Now I gotta try fixing my thing. Thanks for giving me more work. :stuck_out_tongue_winking_eye:

1 Like

Still waiting for the more editor buttons script



It’s live.
Anything you feel is missing, let me know.



1 Like

A lot of closed threads seem to be popping up in the “latest topics” thing all of a sudden…(or not closed, since I could post. Why is the message and lock symbol appearing on threads that aren’t closed?)

I’m cleaning up a problem with the auto-close feature that incorrectly closed a bunch of stuff. The topics should get all reset and stuff as the script runs through things.

Long story short: ignore me for a few minutes…


omg thank you!