# window.addEventListener('load', functionName) not working properly

**URL:** <https://meta.discourse.org/t/window-addeventlistener-load-functionname-not-working-properly/84167>\
**Category:** Development\
**Created:** [March 29, 2018, 8:21pm UTC](https://meta.discourse.org/t/window-addeventlistener-load-functionname-not-working-properly/84167 "2018-03-29T20:21:29Z")\
**Posts on this page:** 1\
**Showing post:** 2

<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:** [March 30, 2018, 2:25am UTC](https://meta.discourse.org/t/window-addeventlistener-load-functionname-not-working-properly/84167/2 "2018-03-30T02:25:24Z")

</div>

If you like to call some JavaScript whenever a user navigated in Discourse then you can use `onPageChange` method.

> [@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/window-addeventlistener-load-functionname-not-working-properly/84167)._
