I am trying to use a hook_init within my custom module to add a js file. The problem is I do not see where the script is being called or executed on the front-page.

/* Implements hook_init. */ 
function hook_init() { 
  if (drupal_is_front_page()) { 
      drupal_add_js(drupal_get_path('modules', 'myInfo') . '/myScript.js'); 

name = My Info 
description = My Info Evaluation. 
core = 7.x 
version = 7.x-0.1-dev

I even tried to remove the hook_init and referenced the JS within the module info, but no luck. What am I missing here?

  • did you find what your problem was? – greggles Aug 31 '15 at 15:31

Some suggestions:

  • Is the module enabled? Silly question, but I've forgotten to do that before.
  • hook_init implementations won't get called until you clear the cache. Did you clear the cache?
  • I will sometimes put a drupal_set_message('this line'); in various parts of the code to see if that's getting called. In situations where a drupal_set_message isn't helping, I sometimes will put a asdf() so I can get a "fatal error, call to undefined function" message. Then it's really clear whether or not the code is getting called.
  • If the hook_init is not being called and the module is enabled and cache was cleared, then it's probably mis-spelled. The function should be named based on the name of your module: If your module is mymodule.module then the function name is mymodule_init().
  • It's possible that the path you are using isn't quite right. Does the file really exist there? I would try: drupal_set_message(drupal_get_path('modules', 'myInfo') . '/myScript.js'); so you can see the path and confirm that it's right.
  • There's some good documentation on Managing JavaScript in Drupal 7 though perhaps you've already read that page.

Good luck!

| improve this answer | |
  • The first thing I did was clear the cache and made sure it was enabled, so those are not the issue. So I will try the others, thanks. – Rseeb Aug 25 '15 at 17:19

In your module, did you replace 'hook' with your module name?

Assuming you have myinfo.info and myinfo.module, the function should be myinfo_init() in order to be called when your module is active.

| improve this answer | |
  • This is probably the right answer! I overlooked that in my list of things. – greggles Aug 25 '15 at 22:59

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.