1

On a fairly new drupal (7.23) site I tried including the jquery-2.0.3.js file with the drupal_add_js function in one of my modules hook_init function. All was working good and well when I was testing the login screen but then I went to some others pages, I got this error in firebug console:

TypeError: $(...).add(...).filter(...).once is not a function
http://XXX/sites/all/libraries/jquery/jquery-2.0.3.js?mw5wsb
Line 1670

(where XXX is my sitename)

So I noticed on one of my other sites the same library loaded fine when it was included in the theme.info file. So I tried it that way and sure enough jquery-2.0.3 loads fine when I do it that way. (ie.

scripts[] = js/vendor/jquery-2.0.3.js

Does anyone know why a jquery include would not work with drupal_add_js and why it would work with the theme.info file?

Many thanks.

d

2
  1. Generally speaking you should avoid adding third-party libraries directly from your module. If all modules would do that, your site would end up with multiple loads of jQuery/Modernizr/Backbone/etc.
    First see if there is an existing module for that, and if not - use Libraries API to declare a dependency in your custom module and then load the script afterwards. This way if some other module depends on the same library and declares dependency through Libraries API, Drupal will load only one file automatically, avoiding conflicts.

  2. For jQuery, check out jQuery Update module. Drupal itself uses jQuery for forms and other elements, so it is not enough that only your module or theme work on the latest jQuery. All core widgets, effects and AJAX calls need to be ported for specific version. That's why jQuery Update module exists - it upgrades the version of jQuery and loads updated Drupal scripts instead of the original ones, so the site does not break.

  3. Regarding the differences between drupal_add_js() and adding a script filename in the theme.info, here's a summary:

    • When you add a script to theme.info file, it will be automatically loaded for all pages displayed with your theme. This means that if you are using an administrative theme, such as Rubik, your script will not be loaded on pages where you use that theme.
    • With drupal_add_js() you can load a JS file only when you need it (e.g. only on certain pages, for certain roles etc.).
    • drupal_add_js() also provides you with additional functionality, such as inline JS for variables.

Hope this helps.

  • Many thanks Top. I'll checkout the libraries and jquery update modules. For my purposes I only needed a (FormLabel) js file loaded for the login and password pages so I didn't want to load jquery 2.0.3 if not necessary. This FormLabel js file required jquery 2.0.3 but the rest of my site didn't. But I see your point about multiple loads of libraries. thanks again. – Dan Shumaker Nov 15 '13 at 22:56
  • You're welcome. If the above solved/answered your question, mark it as an answer so it is useful for other people with the same issue. – Aram Boyajyan Nov 16 '13 at 13:14

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.