How can we add an external javascript library that relies on jQuery by calling $(document)??

As Drupal 7's jQuery needs to be encapsulated, adding the library just generates errors:

$(...) is not a function

I don't understand the point of not being able to use $ directly anymore. And adding jQuery as an external library seems to conflict with the Drupal's one

Thanks for the help

  • 1
    What are you doing with the library? I ask, because if you're using it within your custom code, you should call it from within a function that's already name spaced and there shouldn't be an issue. The reason for not being able to use $ without name spacing it is to avoid conflicting with other JavaScript frameworks Jul 5 '13 at 16:36
  • Also what library are you using. It may be that library is poorly written and it assumes global $ pointing to jQuery. Jul 5 '13 at 18:22
  • This is the library I wanted to add: github.com/ftlabs/fastclick "it assumes global $ pointing to jQuery" is interesting. Does it mean there is way to execute jQuery from firebug for example? So far since Drupal 7 I havent been able
    – ErikaW
    Jul 8 '13 at 8:22
  • 1
    have you tried jQuery(...) instead? Jan 10 '14 at 4:58
  • The fastclick library you referenced does not require jQuery, so you shouldn't need to write any jQuery code to initialize the library.
    – jwal
    Jan 18 '14 at 22:55

You can add javascript library in your custom theme in preprocess-html.inc e.g.

 drupal_add_js(drupal_get_path('theme', 'themename') . '/js/fastclick.min.js');

Use drupal_add_js() for this.

Please also check these resources; they could help you out.

For using other versions of jQuery, there is the jQuery Update module.

  • Hi, thank you for the links. I have actually added it via the theme.info, drupal_add_js, and dirty injection in template.php. Always the same result. I believe it has something to do with what Ihor said: "(the library) assumes global $ pointing to jQuery"
    – ErikaW
    Jul 8 '13 at 8:26

Give this a try, it will assign $ to be jQuery. It's not the correct way to do this, but it seems like that 3rd party library did things wrong to begin with.

drupal_add_js('$ = jQuery;', array(
  'type' => 'inline', 
  'scope' => 'header', 
  'group' => JS_LIBRARY,
  'weight' => -1,

There is a module that does roughly the same thing: https://drupal.org/project/jquery_dollar


Ok, it sounds like there may be two questions here, external libraries and using the dollar sign jQuery variable.

1) I use the Libraries API module to add external libraries that my custom modules need. It makes your code a little more concise I suppose: https://www.drupal.org/project/libraries

Note: I'd add that I've never had a 3rd parties jQuery mess with Drupal's jQuery using the Libraries API.

2) I use the following code to prevent my jQuery code from conflicting with Drupal's jQuery in my module and theme jQuery:

(function ($) { 

    // Do something


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.