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.

8
  • 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
    Commented Mar 30, 2018 at 12:58
  • How are you adding the scripts? This will determine the solution.
    – Jaypan
    Commented Mar 30, 2018 at 13:44
  • @Jaypan in the library.yml Commented Mar 30, 2018 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. Commented Mar 30, 2018 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
    Commented Mar 30, 2018 at 23:14

2 Answers 2

4

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.

4

As mentionned above, one way to control the order is to declare in your library the dependencies of other libraries containing the js you want to be loaded before

agenda:
  version: 1.x
  css:
    component:
      css/agenda.css: {}
  js:
    js/agenda.js: {}
  dependencies:
    - fullcalendar_view/fullcalendar

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.