Execute JavaScript code from a plugin once after load

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.

// 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!

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

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

Render a modal conditionally given some user custom_fields. The work is related to this Force password change after login

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.

2 Likes

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

1 Like