# 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 نوفمبر 2017، 4: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:** 20
**Page:** 1

<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: [27 نوفمبر 2017، 4:36ص UTC](https://meta.discourse.org/t/how-do-you-force-a-script-to-refire-on-every-page-load-in-discourse/74856/1 "2017-11-27T04:36:06Z")

</div>

One the most amazing things about Discourse is that it is a Single Page Application. The page is loaded once and the content is swapped on demand - This is my understanding.

I was trying to integrate [Masonry](https://masonry.desandro.com/) with Discourse and ran into a small problem.

I got Masonry to load great on first page load and everything works great:

 ![Untitled7](https://global.discourse-cdn.com/meta/original/3X/f/8/f839517530566dee843e9a367f2f3a2772f627df.jpg)

However, if I navigate to another topic-list page or select a category then it does not work anymore and I get this:

 ![Untitled8](https://global.discourse-cdn.com/meta/original/3X/c/d/cd97a15dfc2ea536ac1dba6f75c4d973ab4d7b44.jpg)

The method I use to call Masonry is to add the script below to the `</head>`

```
<script>
$(".topic-list").imagesLoaded(function() {
    $(".topic-list").masonry({
      itemSelector: ".topic-list-item"
    });
  });
</script>

```

* * *

I understand why the issue occurs - I think. It occurs because the script is only loaded and fired on initial page load.

This means that the `<head>` element is not reloaded on every page load.

So, logically, my next step was to see if I can add the script somewhere where it will get run again when new content loads.

So I tried

- `</body>`
- `<footer>`

Without luck. For some reason the script never fires again after initial page load.

**So, my question is how can I make sure this script runs on every page load? (for now… selective logic will come in later)**

```
<script>
$(".topic-list").imagesLoaded(function() {
    $(".topic-list").masonry({
      itemSelector: ".topic-list-item"
    });
  });
</script>

```

---

<div class="post-metadata">

### Author: ![vinothkannans](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/vinothkannans/32/86465_2.png) [@vinothkannans](https://meta.discourse.org/u/vinothkannans)
#### Post date: [27 نوفمبر 2017، 5:44ص UTC](https://meta.discourse.org/t/how-do-you-force-a-script-to-refire-on-every-page-load-in-discourse/74856/2 "2017-11-27T05:44:37Z")

</div>

[https://github.com/discourse/discourse/blob/master/app/assets/javascripts/discourse/initializers/page-tracking.js.es6#L11-L27](https://github.com/discourse/discourse/blob/master/app/assets/javascripts/discourse/initializers/page-tracking.js.es6#L11-L27)

Look at the above script. This is how we call Google Analytics on every page reload.

---

<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: [27 نوفمبر 2017، 6:44ص UTC](https://meta.discourse.org/t/how-do-you-force-a-script-to-refire-on-every-page-load-in-discourse/74856/3 "2017-11-27T06:44:46Z")

</div>

That’s a great idea @vinothkannans Thanks!

Might take a whole bunch of time to replicate the method with the script I intend to use (due to my amazing scripting skills 🙃) but I’m now glad I found an official example to lean back on.

Will update this post if I get anywhere.

Thank you!

---

<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: [27 نوفمبر 2017، 2:26م UTC](https://meta.discourse.org/t/how-do-you-force-a-script-to-refire-on-every-page-load-in-discourse/74856/4 "2017-11-27T14:26:24Z")

</div>

So, I spent a bit of time looking at this and I still do not have a proper solution, however I have found a temporary workaround.

I have found that [.ajaxComplete](http://api.jquery.com/ajaxcomplete/) is triggered in Discourse when new content is loaded.

_Small issue: It’s triggered three times on every page load._

Ignoring that small hick up for now (remember… this is a workaround), I modified the code to fire on `.ajaxComplete`

Final code for the workaround:

```
$( document ).ajaxComplete(function() {
$(".topic-list").imagesLoaded(function() {
    $(".topic-list").masonry({
      itemSelector: ".topic-list-item"
    });
  });
}); 

```

The above script runs on every page load in Discourse and that’s what I wanted to achieve.

Masonry now works well on initial and subsequent page loads.

I will continue to investigate and will post a more proper method one I have something solid.

---

<div class="post-metadata">

### Author: ![simon](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/simon/32/339122_2.png) [@simon](https://meta.discourse.org/u/simon)
#### Post date: [27 نوفمبر 2017، 8:01م UTC](https://meta.discourse.org/t/how-do-you-force-a-script-to-refire-on-every-page-load-in-discourse/74856/5 "2017-11-27T20:01:06Z")

</div>

I think you can use the PluginAPI `onPageChange` function to do what you are looking for. See: [Using the JS API](https://meta.discourse.org/t/using-the-pluginapi-in-site-customizations/41281)

Here’s an example function:

```js
<script type="text/discourse-plugin" version="0.8">
    api.onPageChange(() =>{
        randBackground();
    });
    
    function randBackground() {
        $('body').css('background-color', '#'+(Math.random()*0xFFFFFF<<0).toString(16));
    }
</script>

```

* * *

**Update:** If you are looking to apply similar changes to post bodies, see [post #26 below](https://meta.discourse.org/t/how-do-you-force-a-script-to-refire-on-every-page-load-in-discourse/74856/26).

---

<div class="post-metadata">

### Author: ![Mittineague](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/mittineague/32/114259_2.png) [@Mittineague](https://meta.discourse.org/u/Mittineague)
#### Post date: [27 نوفمبر 2017، 8:03م UTC](https://meta.discourse.org/t/how-do-you-force-a-script-to-refire-on-every-page-load-in-discourse/74856/6 "2017-11-27T20:03:44Z")

</div>

Do you need to use “document” as the base? AFAIK, that will have a lot more events depending on how often and what polling returns. I think if you can focus it more to a page element it should work better for you.

---

<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: [28 نوفمبر 2017، 2:03ص UTC](https://meta.discourse.org/t/how-do-you-force-a-script-to-refire-on-every-page-load-in-discourse/74856/7 "2017-11-28T02:03:11Z")

</div>

Ha! 😁 That’s the one!

That’s the proper solution I’ve been trying to work out. Thank you so much @simon.

I modified my hacky workaround to the the method you highlighted and everything works like magic now 😀

The current version I have is

```
<script type="text/discourse-plugin" version="0.8">
api.onPageChange(() => {
  msnrylayout();
});

function msnrylayout() {
  $(".topic-list").imagesLoaded(function() {
    $(".topic-list").masonry({
      itemSelector: ".topic-list-item"
    });
  });
}
</script>

```

Which does what I need it to do in terms of firing once on every page load.

My next step is apply a bit of selective logic so that it does not fire on pages where there is no topic-list.

Will post updates as I get there.

---

<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: [28 نوفمبر 2017، 2:08ص UTC](https://meta.discourse.org/t/how-do-you-force-a-script-to-refire-on-every-page-load-in-discourse/74856/8 "2017-11-28T02:08:13Z")

</div>

> [@Mittineague](#):
>
> Do you need to use “document” as the base? AFAIK, that will have a lot more events depending on how often and what polling returns. I think if you can focus it more to a page element it should work better for you.

You’re totally correct, the “document” is a very broad base. I was trying to find a more specific element but could not for `.ajaxComplete`

Regardless, I think I won’t be needing that script anyway because @simon was kind enough to highlight a method for firing a script with the pluginAPI on page change, which is a lot cleaner and a lot more efficient.

---

<div class="post-metadata">

### Author: ![simon](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/simon/32/339122_2.png) [@simon](https://meta.discourse.org/u/simon)
#### Post date: [28 نوفمبر 2017، 5:56ص UTC](https://meta.discourse.org/t/how-do-you-force-a-script-to-refire-on-every-page-load-in-discourse/74856/9 "2017-11-28T05:56:27Z")

</div>

> [@lll](#):
>
> My next step is apply a bit of selective logic so that it does not fire on pages where there is no topic-list.

The `onPageChange` function is passed a `url` parameter. You might be able to use it to check what page you’re on.

```js
<script type="text/discourse-plugin" version="0.8">
    api.onPageChange((url) => {
        if (url === '/' || url === '/latest' || url === '/top') {
            randBackground();
        }
    });
    
    function randBackground() {
        $('body').css('background-color', '#'+(Math.random()*0xFFFFFF<<0).toString(16));
    }
</script>

```

There is also a PluginApi function for reopening a class. Reopening the `topic-list` class and hooking into `didInsertElement` would probably work.

```js
<script type="text/discourse-plugin" version="0.8">
    api.modifyClass('component:topic-list', {
        didInsertElement() {
            this._super(); // just in case
            randBackground();           
        }
    });
    
    function randBackground() {
        $('body').css('background-color', '#'+(Math.random()*0xFFFFFF<<0).toString(16));
    }
    
</script>

```

---

<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: [29 نوفمبر 2017، 12:28م UTC](https://meta.discourse.org/t/how-do-you-force-a-script-to-refire-on-every-page-load-in-discourse/74856/10 "2017-11-29T12:28:48Z")

</div>

This is amazing @simon I never would’ve thought it to be so flexible! 😍

I really like the first method and I will proceed with that.

I will play around a bit with url based filtering and if everything works well with single urls (`/latest`), (`/new`) and combined (`/tags/c/site-feedback/foo`), (`/c/site-feedback/`), I will update this post again with the what I have.

😀

---

<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: [30 نوفمبر 2017، 3:17ص UTC](https://meta.discourse.org/t/how-do-you-force-a-script-to-refire-on-every-page-load-in-discourse/74856/11 "2017-11-30T03:17:01Z")

</div>

Small update:

So I was trying to make masonry work inside posts to create a gallery if image uploads follow certain structure and it’s something like this:

```
<span>
</br>
<div class=lightbox-wrapper">...</div>
<div class=lightbox-wrapper">...</div>
<div class=lightbox-wrapper">...</div>
....
</span>

```

This will result in the images being organized by masonry to look like this:

 ![Untitled](https://global.discourse-cdn.com/meta/original/3X/f/f/ff0c3e185eba93c20716d9ce41b3a5ef2415fd5d.jpg)

However, clicking any of the images will open the native discourse lightbox, which I like very much!

My issue this time is similar to the issue in the original post, where the script is fired if page is visited directly or on initial page load but not if reached after visiting any other page on discourse.

The script I am using is:

```
<script type="text/discourse-plugin" version="0.8">
api.onPageChange(() => {
  mgallery();
});

function mgallery() {

var $mgallery = $(".cooked>span").has(".lightbox-wrapper+.lightbox-wrapper");

$mgallery.addClass("mgallery");
$mgallery.css("visibility", "hidden");

$mgallery.imagesLoaded(function() {
  $mgallery.masonry({
    itemSelector: ".lightbox-wrapper"
  }).css("visibility", "visible");
});

console.log("mgallery loaded");

};

</script>

```

After every page load I see “mgallery loaded” in the console like expected but the issue persists.

In summary,

**this method of loading a script via the pluginAPI seems to work really well if used for masnory in the topic list pages, however, it does not seem to work in post pages.**

**What should I be looking at next?**

---

<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: [4 ديسمبر 2017، 11:12ص UTC](https://meta.discourse.org/t/how-do-you-force-a-script-to-refire-on-every-page-load-in-discourse/74856/12 "2017-12-04T11:12:51Z")

</div>

Still having the same issue in getting the script to fire on `/t/` pages after initial load.

Here’s what happens,

I visit the post page directly, script fires, gallery is arranged via masonry and I get the console log message I expect.

I navigate within the app, then return to post page (not initial page load anymo) the script does fire and I get the console message but masonry is not actually working.

Here’s the code I’m using now.

**HTML**

```plaintext
<div>
    <br>
    uploaded image 
    uploaded image
    uploaded image
    ....
</div>

```

hacky, I know 😋  
still need to understand how to create custom `[foo]...[/foo]` tags  
or white list some html `<div class="foo">...</div>`

* * *

**CSS**

not relevant here so I will skip it.

* * *

**JS:**

```plaintext
<script type="text/discourse-plugin" version="0.8">
    api.onPageChange(() => {
      mgallery();
    });

    function mgallery() {

    var $mgallery = $(".cooked>div:not('[class]'):not('[id]')").has(".lightbox-wrapper+.lightbox-wrapper");

    $mgallery.addClass("mgallery");
    $mgallery.css("visibility", "hidden");

    $mgallery.imagesLoaded(function() {
      $mgallery.masonry({
        itemSelector: ".lightbox-wrapper"
      }).css("visibility", "visible");
    });

    console.log("mgallery loaded");

    };
</script>

```

Everything works on initial page load:

 ![Capture](https://global.discourse-cdn.com/meta/original/3X/7/5/75e29d6740b323b97d4d893b4aa041bb96443587.jpg)

but not on subsequent page loads.

 ![Capture1](https://global.discourse-cdn.com/meta/original/3X/5/2/52f27f4c642b9b5cdb954f5b3cd8b5211633777f.jpg)

even when the console log indicates that the script has re-fired.

* * *

The confusing part is that I use the same method to initialize masonry on topic-list pages and it **works just fine on both initial and subsequent page loads**

 ![Capture3](https://global.discourse-cdn.com/meta/original/3X/0/3/0327dd4836b6010cdd31f9b362e87dbea0555c95.jpg)

using this script:

```plaintext
<script type="text/discourse-plugin" version="0.8">
    api.onPageChange(() => {
      msnrylayout();
    });

    function msnrylayout() {

    $(".topic-list").css("visibility", "hidden");
    $(".topic-list").imagesLoaded(function() {
      $(".topic-list").masonry({
        itemSelector: ".topic-list-item"
      }).css("visibility", "visible");
    });

    console.log("Masonry loaded");

    };
</script>

```

**Is there anything special about topic pages** (`/t/`) **- when compared to topic-list pages - that would prevent masonry script from working as intended on every post page load within the app?**

---

<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: [14 ديسمبر 2017، 10:12ص UTC](https://meta.discourse.org/t/how-do-you-force-a-script-to-refire-on-every-page-load-in-discourse/74856/14 "2017-12-14T10:12:16Z")

</div>

This may or may not be a bug. I cannot really tell.

## Problem:

jQuery scripts do not fire `onPageChange` when the destination is a topic as consistently as they do when the destination is a discovery route.

## Basic example script:

```plaintext
      api.onPageChange((url, title) => {
        console.log('the page changed to: ' + url + ' and title ' + title);
        $(".regular>.cooked>p").addClass("foo-class");
      });

```

## Expected result:

On every page loaded, the page title and url are logged to console and every `.regular>.cooked>p` element gets the class `.foo-class` added to it.

And this is exactly what happens on initial page load (if topic is accessed via direct url)

 ![screenshot with the script loading well on initial page load](https://global.discourse-cdn.com/meta/original/3X/1/4/145aa696e37dc6814e87d3a626ae544cc8c73a9f.jpg)

As you can see above, every `<p>` tag has the class `.foo-class` added to it and the console logs the page information as instructed.

* * *

## Actual result

If you navigate away from the topic and go back in or if you go visit another topic by clicking a link from within the app (where the content is loaded via the app), the script behaves differently.

The console logs the page information like before, but the jQuery part is not picked up.

notice how the `<p>` tags don’t have the class `.foo-class` added even though the console indicates that the script was executed

 ![screenshot of the problem where jquery script is not executed as expected](https://global.discourse-cdn.com/meta/original/3X/9/1/918e290e78147aff2c4cba0e6cc3d9dcec12eb6d.jpg)

* * *
For the record, here's the full script and I add it to the `` section of the common theme:

```
<script type="text/discourse-plugin" version="0.8.13">
      api.onPageChange((url, title) => {
        console.log('the page changed to: ' + url + ' and title ' + title);
        $(".regular>.cooked>p").addClass("foo-class");
      });
</script>

```

* * *

I also went through the entire `plugin-api.js` file and did not find any alternative to `onPageChange`

[https://github.com/discourse/discourse/blob/master/app/assets/javascripts/discourse/lib/plugin-api.js.es6](https://github.com/discourse/discourse/blob/master/app/assets/javascripts/discourse/lib/plugin-api.js.es6)

---

<div class="post-metadata">

### Author: ![awesomerobot](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/awesomerobot/32/142900_2.png) [@awesomerobot](https://meta.discourse.org/u/awesomerobot)
#### Post date: [21 مارس 2018، 8:19م UTC](https://meta.discourse.org/t/how-do-you-force-a-script-to-refire-on-every-page-load-in-discourse/74856/15 "2018-03-21T20:19:19Z")

</div>

> [@lll](#):
>
> jQuery scripts do not fire onPageChange when the destination is a topic as consistently as they do when the destination is a discovery route.

Did you ever get to the bottom of this? I was trying to do something simple with jQuery earlier and it seems like api.onPageChange isn’t the right solution.

---

<div class="post-metadata">

### Author: ![tshenry](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/tshenry/32/119495_2.png) [@tshenry](https://meta.discourse.org/u/tshenry)
#### Post date: [21 مارس 2018، 9:00م UTC](https://meta.discourse.org/t/how-do-you-force-a-script-to-refire-on-every-page-load-in-discourse/74856/16 "2018-03-21T21:00:20Z")

</div>

I have been working on something that needs to be fired when entering a topic. I don’t know if this is overly complicated or the proper way to do it, but this is what I’ve found works well so far:

```plaintext
<script type="text/discourse-plugin" version="0.8.13">
    api.addPostTransformCallback((t) => {
        // post is topic post && element doesn't already exist
        if (t.post_number === 1 && $("#test-id").length == 0) {
            $(function() {
                var testText = '<h1 id="test-id">Test</h1>';
                $("#post_1 .regular.contents").before(testText);
            });
        }
    });
</script>

```

Try this out in a theme component. It will add the word “Test” to the first post in every topic.

---

<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: [22 مارس 2018، 3:21ص UTC](https://meta.discourse.org/t/how-do-you-force-a-script-to-refire-on-every-page-load-in-discourse/74856/19 "2018-03-22T03:21:08Z")

</div>

> [@awesomerobot](#):
>
> Did you ever get to the bottom of this?

Yes and no, I don’t know why topic pages seem to behave differently here but I have found a couple of workarounds.

~~If you want to do something only once when a topic page is loaded, then matching the url for `/t/` seems to work.~~

 

~~Like so:~~

~~ 

```plaintext
<script type="text/discourse-plugin" version="0.8.18">
api.onPageChange(url => {
    // if url matches */t/* or topic page
	if (url.match(/^\/t\//)) {
		// do something
		console.log("foobar");
	}
});
</script>

```

 ~~

**Edit:**

* * *

_Nope 😅, this will still log stuff to console but still very finicky about everything else and will only work with a jQuery function if the page is the first the user views and not on in-app navigation this is either a bug or something is running out of order. I don’t see any errors in the log though_

For this use case @tshenry’s method works really well. 😁👌 So I’m adapting it going forward.

**The method below still works though for Ajax content**

* * *

However, if you want to modify content including content that’s loaded later on after scrolling, you’d need a slightly more complicated workaround.

This is not entirely clean but still a little bit efficient. It could still be vastly improved.

in brief, you bind whatever you want to do to an `ajaxSuccess` handler while on topic pages and then unbind after leaving topic pages.

```plaintext
<script type="text/discourse-plugin" version="0.8.18">
api.onPageChange(url => {
	fo_bar = fooBar();
	// if url matches */t/*
	if (url.match(/^\/t\//)) {
		fooBar();
	} else {
	    // if not a topic page, unbind the ajaxSuccess handler
		$(document).unbind("ajaxSuccess", fo_bar);
	}
});

function fooBar() {
	$(document).ajaxSuccess(function() {
		// do stuff
		console.log("foobar");
	});
}
</script>

```

This will fire 2-3 times when entering a topic and when new content is loaded while on topic pages, but will not fire outside of topic pages. This is still far from ideal but so far is the only way I’ve manged to do this and make changes to content loaded later on after scrolling.

One possible room for improvement is to move the unbind function from the url based `if` `else` statement to a `cleanupStream` function which would only fire once when exiting a topic page. However, I still have not figured out how to fully use it.

For reference it’s here:

[https://github.com/discourse/discourse/blob/master/app/assets/javascripts/discourse/lib/plugin-api.js.es6#L332-L340](https://github.com/discourse/discourse/blob/master/app/assets/javascripts/discourse/lib/plugin-api.js.es6#L332-L340)

---

<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 مارس 2018، 3: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.

---

<div class="post-metadata">

### Author: ![tshenry](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/tshenry/32/119495_2.png) [@tshenry](https://meta.discourse.org/u/tshenry)
#### Post date: [24 مارس 2018، 3:35ص UTC](https://meta.discourse.org/t/how-do-you-force-a-script-to-refire-on-every-page-load-in-discourse/74856/21 "2018-03-24T03:35:34Z")

</div>

This is really interesting! Definitely different than anything I’ve worked with in theme dev so far. Looking forward to checking out the breadcrumbs and trying to get a deeper understanding of what all is going on. Thanks for the update 😀

---

<div class="post-metadata">

### Author: ![Mittineague](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/mittineague/32/114259_2.png) [@Mittineague](https://meta.discourse.org/u/Mittineague)
#### Post date: [24 مارس 2018، 3:47ص UTC](https://meta.discourse.org/t/how-do-you-force-a-script-to-refire-on-every-page-load-in-discourse/74856/22 "2018-03-24T03:47:43Z")

</div>

> [@lll](#):
>
> how to fire a script once every time the post stream is updated

Maybe try this?

> **[ajaxComplete event | jQuery API Documentation](https://api.jquery.com/ajaxcomplete/)**

---

<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 مارس 2018، 3:50ص UTC](https://meta.discourse.org/t/how-do-you-force-a-script-to-refire-on-every-page-load-in-discourse/74856/23 "2018-03-24T03:50:55Z")

</div>

You’re correct @Mittineague and that’s what I’m using now.

There’s only one small hick up which is I cannot seem to get it to work with anything except for the entire `document` which as you may have already guessed it, would mean it fires 2 or three times each time more posts are loaded into the post stream.

It would be really great if I can use it with the post stream as the base but so far I have not been able to do so.

However, my testing my be a bit off so I will try this again 👌

[Next page](https://meta.discourse.org/t/how-do-you-force-a-script-to-refire-on-every-page-load-in-discourse/74856.md?page=2)
