2

I am using mixitup.js for my custom module. But it gives error when firing because it needs to load the jQuery before loading the mixitup.js. But my jQuery files are in my site folder which are loading in the end of the page. Is there way to load my mixitup.js in the end of the page. mixitup.js should be in my custom module too.

  • What do you mean with "...in site folder..."? jQuery is part of Drupal's core, just add a dependency to core/jquery it in your module's library.yml file, you don't need any manual load ordering. – Hudri Mar 30 '18 at 12:58
  • How are you adding the scripts? This will determine the solution. – Jaypan Mar 30 '18 at 13:44
  • @Jaypan in the library.yml – i am batman Mar 30 '18 at 15:46
  • @Hudri Yes. I have already added jQuery to libraries.yml. But that too loads in the end of the page. Not on the top. I need to load all the javascripts at the top of the page. – i am batman Mar 30 '18 at 15:47
  • 1
    See sonfd's reply below. That's the way to do it. If you set jquery as a dependeny, jQuery will load above it. – Jaypan Mar 30 '18 at 23:14
3

To add a js library to the header, define your library in my_module.libraries.yml with the header: true property.

my_library:
  header: true
  etc ...

Any dependencies defined for your library will always be loaded before your library whether loading your js in the header or footer.

See Asset loading order section here.

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.