Tell me more ×
Drupal Answers is a question and answer site for Drupal developers and administrators. It's 100% free, no registration required.

I’ve got a question about module development that I need some help with.

I’m building a module which needs to have multiple JS files connected to it. The first file, which has the same name as the module works just fine. But the rest of the JS files, attached the same way are being ignored. For example:

function mymodule_init() {
  drupal_add_js(drupal_get_path('module', 'mymodule') .'/js/mymodule.js');
  drupal_add_js(drupal_get_path('module', 'mymodule2') .'/js/mymodule2.js');
}

I’m not sure what I’m doing wrong, if anyone has a suggestion or can point me to some documentation it would be appreciated.

Thanks Stephanie

share|improve this question
add comment (requires an account with 50 reputation)

1 Answer

up vote 1 down vote accepted

It looks like it's due to the second implementation of drupal_get_path().

You're passing through mymodule2 as the argument, where it should just be mymodule.

e.g.

function mymodule_init() {
  drupal_add_js(drupal_get_path('module', 'mymodule') .'/js/mymodule.js');
  drupal_add_js(drupal_get_path('module', 'mymodule') .'/js/mymodule2.js');
}

On another note, if the JavaScript needs to be added on every page (as I'm guessing it does with the implementation of hook_init) then you might be better off adding your scripts in using scripts[] in the module's .info file

scripts (Optional)

You can now add Javascript in the module's .info file if it should be added on every page. This allows Javascript to be aggregated in an optimal way, and is the preferred method of adding Javascript that most visitors will need on a typical site visit

Source: Writing module .info files

References:

drupal_get_path(); API Docs

Managing JavaScript in Drupal 7

Writing module .info files

share|improve this answer
I see! Thanks! I'll be trying this later today and let you know how I fair. Thank you! – Stephanie Nov 27 '12 at 15:41
No worries :) If it's all good then don't forget to come back and comment/accept the answer :) – Chapabu Nov 27 '12 at 15:44
Sorry it took so long to reply, thanks again for the help! – Stephanie Jan 3 at 15:26
add comment (requires an account with 50 reputation)

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.