the following is my js file mymodule.js



and i just add this file in my customtheme.info .

scripts[] = js/mymodule.js

However, the js code only be executed at some pages, not all pages.

What could the problem will be?

Does anyone have the same situation before?

Please help me fix this.

2 Answers 2


In Drupal 7 to avoid conflicts with another library you should wrap your code in:

(function ($) {
  // Original JavaScript code.

To learn more visit: http://drupal.org/update/modules/6/7#javascript_compatibility


Try it by implementing hook_init() into your .module file:

function YOURMODULE_init() {
    drupal_add_js(drupal_get_path('module', 'YOURMODULE')  ."/YOURSCRIPT.js",  array('every_page' => TRUE));

Also make sure your module is in its proper place: sites/all/modules/YOURMODULE or a subfolder of this.

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.