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

I have written my custom module for Drupal 7. In my page_change.module file I have only:

<?php
    drupal_add_js(drupal_get_path('module', 'page_change') . '/page_change.js');
?>

In my page_change.js file I have only:

Drupal.behaviors.page_change = {
    attach: function (context) {
        console.log("text");
    }
};

As soon as I enable my module, Drupal doesn't load jquery.js. It does not even appear in html source in <head> section. When I disable my module, or delete the only line from page_change.module file everything works fine.

I have also tried to load my javascript file by adding

scripts[] = page_change.js

In my page_change.info file (and leaving page_change.module with only <?php ?>) but it doesn't seem to work either.

Why does that happen? What am I doing wrong? Am I missing something?

edit: I have even tried to add inline javascript by adding

drupal_add_js('console.log("text");', array('type' => 'inline', 'scope' => 'footer'));

and it writes text to console, but jquery.js is still missing.

share|improve this question

2 Answers

Your custom JS should be wrapped in:

(function($) {
  // custom Drupal behaviors here
})(jQuery);

Give that a shout.

share|improve this answer
 
I do not use jQuery in my javascript. I've tried this, it doesn't help. Problem is that Drupal does not load jQuery when I add any javascript, not the javascript itself. Javascript works perfectly. –  Crazy Yoghurt Sep 12 at 17:32
up vote 0 down vote accepted

Ok, somehow I forced it to work, but I still have no idea why it hadn't work before.

I have changed my page_change.module contents to:

<?php
function page_change_preprocess_page(&$variables) {
    $file = drupal_get_path('module', 'page_change').'/page_change.js';
    $options = array(
      'weight' => 1000,
      'scope' => 'footer'
    );
    drupal_add_js($file, $options);
}
?>

then I disabled and enabled my module and miraculously it works.

If you have any idea why I had these problems, please post your answer.

share|improve this answer

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.