I need to add a js file and make it to be executed as last js file (after all js that belong to modules)

I create a module for this. But I can't manage the order of modules execution. Don't I? ¿should I add this file in my theme or through a function instead?


Using drupal_add_js you can control the order of your JS files by assigning weight through $options array.

weight: A number defining the order in which the JavaScript is added to the page relative to other JavaScript with the same 'scope', 'group', and 'every_page' value.

The exact ordering of JavaScript is as follows:

  1. First by scope, with 'header' first, 'footer' last, and any other scopes provided by a custom theme coming in between, as determined by the theme.
  2. Then by group.
  3. Then by the 'every_page' flag, with TRUE coming before FALSE.
  4. Then by weight.
  5. Then by the order in which the JavaScript was added. For example, all else being the same, JavaScript added by a call to drupal_add_js() that happened later in the page request gets added to the page after one for which drupal_add_js() happened earlier in the page request.

Code example:

drupal_add_js($file_path, array(
  'weight' => 10000, // Something higher than the weight of existing items
  'scope' => 'footer', // Make sure the script tag is rendered in the footer of the page, not the header
  'group' => JS_THEME, // JS_THEME has the highest group weight and will be rendered last
  • 1
    Hope you don't mind I merged the code from my answer with this, didn't seem any sense in keeping them both... – Clive Mar 1 '13 at 14:56

If you want to guarantee that it is the last thing done, you really need to work outside of the Drupal behaviors. You can still load the file wherever you want, but you need to do something slightly different.

Drupal behaviors fire as part of a $(document).ready(). The only way to guarantee that your function will be last is to use a later event like $(window).load(), eg

(function ($, Drupal, undefined) {
  $(window).load(function () {
})(jQuery, Drupal);
  • But how does that prevent another function that binds to $(window).load() from running after this code? You would have to make sure the file is last to be loaded into the HTML. Plus deferring JS until the entire document (and all images) have downloaded is not a good trade off IMO – Clive Mar 1 '13 at 14:33
  • 1
    @Clive Yes, correct on all counts. Relying on execution order for anything is a bad idea, and should be avoided is possible as it is a fragile beast. This solution does assume that nobody remembers that you can use $(window).load() to do things :) While is does delay things, it can help speeds up page loads when you need to load a lot of assets (eg, lots of XML or JSON for a web app), because your assets won't clog up the loading queue for general JS, CSS, and images. This is the only time I still use $(window).load() by choice. – mpdonadio Mar 1 '13 at 14:54

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.