0

I have created a custom module in drupal 7. The module creates a form for user and store the form's data into database. The module is working fine. But I want to add a custom jquery for the page. I searched Google and find the result for adding custom JQuery for theme not for modules. I guessed and add the following in my module's info file

;scripts
scripts[] = js/test.js

Just alert a "hello" word while the module will load. But nothing is happened. Is this possible to add custom Jquery for a module or I am doing wrong? Please help.

closed as off-topic by kiamlaluno Nov 12 '14 at 12:20

This question appears to be off-topic. The users who voted to close gave this specific reason:

  • "This question was caused by a problem that can no longer be reproduced, was solved by a cache clear, or was a simple typographical error. While similar questions may be on-topic here, this one was resolved in a manner unlikely to help future readers." – kiamlaluno
If this question can be reworded to fit the rules in the help center, please edit the question.

  • 2
    It should be worked if you put your code in "test.js" file in "js" directory in your current theme and then clear cache. – Yusef Mohamadi Nov 12 '14 at 8:48
  • I did what you said and it worked. Thanks man... :) – Subhajyoti De Nov 12 '14 at 10:00
  • Since it was just a problem of clearing the cache, I am going to close this question. – kiamlaluno Nov 12 '14 at 12:20
5

There are several ways of adding JS to your module, you can check them here https://www.drupal.org/node/756722

Your attempt should work fine, just don't forget to clear the caches after editing module's .info file.

Cheers!

  • As for reference for future answers, answers that are just links are not considered answers, on Stack Exchange. You should quote the relevant parts in the answer itself, which should not rely on an external link. – kiamlaluno Nov 12 '14 at 12:22
4

with the drupal_add_js function you can add your own javascript file, it is best to call in hook_init

Example

/**
 * Implements hook_init().
 */
function mymodule_init(){
  drupal_add_js(drupal_get_path('module', 'mymodule') . '/js/mymodule.script.js', array('scope' => 'footer')); 
}

drupal_add_js
hook_init

This hook is not run on cached pages. To add JS that should be present on all pages, modules should not implement this hook, but declare these files in their .info file.

scripts[] = js/script.js
  • From hook_init docs page: "This hook is not run on cached pages. To add CSS or JS that should be present on all pages, modules should not implement this hook, but declare these files in their .info file." – Beebee Nov 12 '14 at 9:40
  • 1
    It's done in my comment, Thx Beebee – Abdelouahed Siyagh Nov 12 '14 at 9:56
1

You can add any no of js files and css files from your .info file. This is as simple as you modify your modulename.info file with:

scripts[] = js/jsfilename.js
stylesheets[all][] = css/cssfilename.css
stylesheets['specify_name'][] = css/cssfilename.css

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