So i wan't to load a custom .js file at the very end of the page after all other .js files have been loaded.

This is my library file:

    assets/js/custom.js: {}
    - core/jquery
    - core/drupal
    - core/drupal.dialog.ajax
    - core/toolbar.escapeAdmin

So the lib is loaded in the page footer with all the rest of the .js files, but it's not last, it's somewhere in the middle. I even tried to add the dependency for toolbar.escapeAdmin as it's javascript file is loaded the very last, as you can see from the picture bellow of the of page:viewsource, but it didn't work.

enter image description here

Any ideas?

1 Answer 1


That is neither possible (because JS files have no "weight" anymore in Drupal 8), nor does it make sense: Any module can declare a JS file as async or defered, so even if your file would be in last position in HMTL markup, there is absolutely no guarantee that it will be executed in that order.

PS: Module dependencies seem to quite reliably affect ordering of their JS libs in markup, not really certain about it though.

  • I've got news for you: sometimes we have no choice. We have to use non-Drupal standard JS and sometimes it MUST be loaded last to prevent conflicts. And if you force a library dependency to make something appear last that library will appear on every page the custom library is on, even if it's not needed on that page. This is a genuine problem. Dec 4, 2018 at 14:33
  • That's neither news or nore a matter of choice: It is technically not possible to ensure a JS load order in Drupal. Period. You cannot control when and how other modules load their JS, so the problem can never be solved reliably with load order. If your script requires a certain execution order you have to learn how to handle async JS (in general, this is not Drupal-specific)
    – Hudri
    Dec 4, 2018 at 14:44

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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