Take the 2-minute tour ×
Drupal Answers is a question and answer site for Drupal developers and administrators. It's 100% free, no registration required.

I have a module called catalog_menu

I included a js file within this module in this way:

drupal_add_js(drupal_get_path('module', 'catalog_menu') . '/js/catalog_menu.js');

Now in the browser I get this error message:

Uncaught TypeError: Property '$' of object [object DOMWindow] is not a function

I think that the problem is that catalogo_menu.js is loaded before jquery.js (the jquery library)

What is the right way to load a js file which depends on jquery library?

share|improve this question
    
Are you using Drupal 7? –  drmonkeyninja Feb 17 '12 at 11:55

2 Answers 2

up vote 3 down vote accepted

I actually think it is pretty unlikely that you have a weight problem, unless you redefined your module to have a extremely low weight. Make sure you have JS optimization turned off, and then do a view source and make sure jQuery is the first script loaded (nearly always is).

My two guesses are

  1. You have a JS error somewhere else, and this is a weird side effect.
  2. You have a JS error in your catalog_menu.js (or another file) that is nuking the $ object by accident.

Also, you should you use an anonymous closure or JS module pattern for code, because Drupa 7 defaults jQuery no-conflict mode.

(function ($, Drupal) {
  function init () {
    $("body").addClass("catalog-menu");
  }

  Drupal.behaviors.catalog_menu = {
    attach: init
  };
}(jQuery, Drupal));
share|improve this answer
    
I tried just leaving these lines: (but not working at all, not error message neither) –  chefnelone Feb 22 '12 at 18:56
    
(function ($, Drupal) { function init () { $('.logo-img').hide(); } Drupal.behaviors.catalog_menu = { attach: init }; }(jQuery, Drupal)); –  chefnelone Feb 22 '12 at 18:56
    
working, thanks –  chefnelone Feb 23 '12 at 8:35

You can change your module's weight to ensure it gets loaded after everything else.

Several ways :

  • SQL the system table (update system set weight=9999 where name = 'mymodule')
  • use the Module Weight submodule from the Util module.
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.