how to write inline jquery on page load in elgg 2.0

how to write inline jquery on page load in elgg 2.0

  • You cannot. jQuery gets loaded asynchronously, so it isn't loaded yet by the time the browser attempts to run your inline js.

    Consider writing an AMD module instead and defining jQuery as a dependency for it: http://learn.elgg.org/en/1.12/guides/javascript.html

  • This is not quite true. We've moved jquery to the bottom of the page, but it's still loaded synchronously. That being said, the effect is the same that you can't simply write inline js in the middle of the page.

    Writing an AMD modules is the best idea. You can also use require inline to load jquery:

    require(['jquery'], function(jQuery) {
    // ...
    });

    But really we discourage inline JavaScript because it defeats security measures like CSP and fails to cache well.

    An external AMD module like juho recommended is the best option.