0

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

1 Answer 1

1

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

2
  • I see! Thanks! I'll be trying this later today and let you know how I fair. Thank you!
    – StephanieF
    Commented Nov 27, 2012 at 15:41
  • Sorry it took so long to reply, thanks again for the help!
    – StephanieF
    Commented Jan 3, 2013 at 15:26

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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