Question

I want to customize the order of loading javascripts in drupal 7. I am using Bartik theme. If I want to add a new JS and load it somewhere in between those list. How can I achieve the same? Thanks

Was it helpful?

Solution

You have to use drupal_add_js function

The second parameter is $options, that you can pass the group of your Javascript file

  • JS_LIBRARY: Any libraries, settings, or jQuery plugins.
  • JS_DEFAULT: Any module-layer JavaScript.
  • JS_THEME: Any theme-layer JavaScript.

Also you can pass the weight of your script to define the order you want.

Here is an example

drupal_add_js('jQuery(document).ready(function () { 
  alert("Hello!"); });', 
  array('type' => 'inline', 'scope' => 'footer', 'weight' => 5)
);

I recommend you to read this article: Managing javascript in Drupal 7

OTHER TIPS

If you want one javascript to get load before the other javascripts then, you can write hook_init() in module file and write drupal_add_js to inculde javascript you want to get load first.

and if the javascripts are external then you can directly use in html.tpl file in whatever order you want to load it.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top