# Trigger a jquery function on post load

**URL:** https://meta.discourse.org/t/trigger-a-jquery-function-on-post-load/85003
**Category:** Development
**Created:** [April 10, 2018, 6:23pm UTC](https://meta.discourse.org/t/trigger-a-jquery-function-on-post-load/85003 "2018-04-10T18:23:59Z")
**Posts on this page:** 1
**Showing post:** 3

<div class="post-metadata">

### Author: ![fefrei](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/fefrei/32/119538_2.png) [@fefrei](https://meta.discourse.org/u/fefrei)
#### Post date: [April 11, 2018, 3:43pm UTC](https://meta.discourse.org/t/trigger-a-jquery-function-on-post-load/85003/3 "2018-04-11T15:43:46Z")

</div>

This should help you:

> [@How do you force a script to refire on every page load in Discourse?](https://meta.discourse.org/t/how-do-you-force-a-script-to-refire-on-every-page-load-in-discourse/74856/5):
>
> 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: \<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).

---

_[View the full topic](https://meta.discourse.org/t/trigger-a-jquery-function-on-post-load/85003)._
