# Execute JavaScript code from a plugin once after load

**URL:** https://meta.discourse.org/t/execute-javascript-code-from-a-plugin-once-after-load/85580
**Category:** Development
**Created:** [April 17, 2018, 10:04pm UTC](https://meta.discourse.org/t/execute-javascript-code-from-a-plugin-once-after-load/85580 "2018-04-17T22:04:22Z")
**Posts on this page:** 5
**Page:** 1

<div class="post-metadata">

### Author: ![eatcodetravel](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/eatcodetravel/32/94385_2.png) [@eatcodetravel](https://meta.discourse.org/u/eatcodetravel)
#### Post date: [April 17, 2018, 10:04pm UTC](https://meta.discourse.org/t/execute-javascript-code-from-a-plugin-once-after-load/85580/1 "2018-04-17T22:04:22Z")

</div>

Hello!,

I’m looking a way to execute JavaScript code after Discourse has finished loading. I’m using the following approach as I’ve seen it some plugins.

```js
// Inside an initializer
export default {
  name: 'my-initializer',
  initialize(container) {
    withPluginApi('0.8.9', api => {
      api.modifyClass('component:site-header', {
        @on('didInsertElement')
        myInitializer() {
          Ember.run.scheduleOnce('afterRender', () => {
            // my code here
          });
        },
      });
    });
  },
};

```

Is there a way to do specifically this, without hooking to the site-header component lifecycle? Like a `window.onload` but for Discourse.

Thanks!

---

<div class="post-metadata">

### Author: ![riking](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/riking/32/170938_2.png) [@riking](https://meta.discourse.org/u/riking)
#### Post date: [April 18, 2018, 12:05am UTC](https://meta.discourse.org/t/execute-javascript-code-from-a-plugin-once-after-load/85580/2 "2018-04-18T00:05:24Z")

</div>

Well, they’re hooking into the `site-header` because those plugins need to modify the header 🙂

Can you be a little more specific about what this code needs to do?

---

<div class="post-metadata">

### Author: ![eatcodetravel](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/eatcodetravel/32/94385_2.png) [@eatcodetravel](https://meta.discourse.org/u/eatcodetravel)
#### Post date: [April 18, 2018, 12:07am UTC](https://meta.discourse.org/t/execute-javascript-code-from-a-plugin-once-after-load/85580/3 "2018-04-18T00:07:15Z")

</div>

Render a modal conditionally given some user custom\_fields. The work is related to this [Force password change after login - #5 by riking](https://meta.discourse.org/t/force-password-change-after-login/85202/5)

---

<div class="post-metadata">

### Author: ![riking](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/riking/32/170938_2.png) [@riking](https://meta.discourse.org/u/riking)
#### Post date: [April 18, 2018, 12:08am UTC](https://meta.discourse.org/t/execute-javascript-code-from-a-plugin-once-after-load/85580/4 "2018-04-18T00:08:00Z")

</div>

In that case, you can probably stick the code in `</body>` in a theme component. The base classes e.g. `Discourse.User.current()` exist and are loaded at that point.

---

<div class="post-metadata">

### Author: ![eatcodetravel](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/eatcodetravel/32/94385_2.png) [@eatcodetravel](https://meta.discourse.org/u/eatcodetravel)
#### Post date: [April 18, 2018, 12:10am UTC](https://meta.discourse.org/t/execute-javascript-code-from-a-plugin-once-after-load/85580/5 "2018-04-18T00:10:26Z")

</div>

In my case I want to do it inside a plugin, like what’s the right way to do it from a plugin. I’ll update the title sorry
