# How do you force a script to refire on every page load in Discourse?

**URL:** https://meta.discourse.org/t/how-do-you-force-a-script-to-refire-on-every-page-load-in-discourse/74856
**Category:** Development
**Created:** [27 Novembro , 2017 04:36 UTC](https://meta.discourse.org/t/how-do-you-force-a-script-to-refire-on-every-page-load-in-discourse/74856 "2017-11-27T04:36:06Z")
**Posts on this page:** 1
**Showing post:** 20

<div class="post-metadata">

### Author: ![Johani](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/johani/32/176920_2.png) [@Johani](https://meta.discourse.org/u/Johani)
#### Post date: [24 Março , 2018 03:22 UTC](https://meta.discourse.org/t/how-do-you-force-a-script-to-refire-on-every-page-load-in-discourse/74856/20 "2018-03-24T03:22:27Z")

</div>

Alright, here’s one more update on this. I think I maybe on to the reason why `api.onPageChange` does not work for topic pages and the answer might be this:

> [@zogstrip](#):
>
> We recently refactored our topic views to use virtual-dom

With that in mind here’s the new way I settled on for firing a script once a user enters a topic page and doing a bit of clean up after leaving the topic page. This could still be improved I suppose but for now will only fire once when entering a topic and once when leaving.

```plaintext
const TopicRoute = require('discourse/routes/topic').default;

TopicRoute.reopen({
	activate: function() {
		this._super();
		Em.run.next(function() {
		// do stuff here when a topic page is opend
		});
	},
	deactivate: function() {
		this._super();
		// clean up here when leaving the topic page
	}
});

```

As far as I tested, this will both log stuff to the console and ~~work with jQuery.~~ **edit: Nope. Again 😅**

If anyone is interested in the breadcrumbs I came accross to put this together check these posts / topics out:

> **breadcrumbs**
>
> [https://meta.discourse.org/t/accessing-the-createtopic-action-while-on-a-topic-route/26423](https://meta.discourse.org/t/accessing-the-createtopic-action-while-on-a-topic-route/26423)
> 
> [A tour of how the Widget (Virtual DOM) code in Discourse works - #59 by david](https://meta.discourse.org/t/a-tour-of-how-the-widget-virtual-dom-code-in-discourse-works/40347/59)
> 
> [https://meta.discourse.org/t/using-jquery-to-add-a-link-to-a-group-badge/40563](https://meta.discourse.org/t/using-jquery-to-add-a-link-to-a-group-badge/40563)
> 
> [https://meta.discourse.org/t/customizing-the-home-screen-to-show-tiles-for-topics/16713/2](https://meta.discourse.org/t/customizing-the-home-screen-to-show-tiles-for-topics/16713/2)
> 
> [https://meta.discourse.org/t/how-to-insert-static-welcome-text-block-on-categories-page/16090/5](https://meta.discourse.org/t/how-to-insert-static-welcome-text-block-on-categories-page/16090/5)
> 
> [https://meta.discourse.org/t/wip-list-of-all-the-hooks-in-discourse/11505](https://meta.discourse.org/t/wip-list-of-all-the-hooks-in-discourse/11505)
> 
> [A tour of how the Widget (Virtual DOM) code in Discourse works](https://meta.discourse.org/t/a-tour-of-how-the-widget-virtual-dom-code-in-discourse-works/40347/1)

Once I figure out how to fire a script once every time the post stream is updated I will post another update here.

---

_[View the full topic](https://meta.discourse.org/t/how-do-you-force-a-script-to-refire-on-every-page-load-in-discourse/74856)._
