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

I am trying to add jQuery to my page (it is not being added by default). The file is adding to the page but after drupal.js causing the error:

SCRIPT5009: 'jQuery' is undefined 
drupal.js, line 5 character 1 

The code that adds drupal.js to the page in common.inc

...
'misc/drupal.js' => array(
          'data' => 'misc/drupal.js',
          'type' => 'file',
          'scope' => 'header',
          'group' => JS_LIBRARY,
          'every_page' => TRUE,
          'weight' => -1,
          'preprocess' => TRUE,
          'cache' => TRUE,
          'defer' => FALSE,
        ),
...

In my module hook_init I have:

  drupal_add_js('misc/jquery.js', array(
    'group' => JS_LIBRARY,
    'weight' => -20,
    'cache' => TRUE,
  ));

I would think with a lower weight it would load before drupal.js?

The header of the page contains the following scripts:

<script type="text/javascript" src="http://mysite.com/misc/drupal.js?mqdxrf"></script>
<script type="text/javascript" src="http://mysite.com/misc/jquery.js?mqdxrf"></script>
<script type="text/javascript" src="http://mysite.com/misc/jquery.once.js?mqdxrf"></script>
<script type="text/javascript" src="http://mysite.com/sites/default/files/languages/nl_c9aC92_wtuULa79-hTFcGPM53oqk1yKwciOa_Q3yO28.js?mqdxrf"></script>
<script type="text/javascript" src="http://mysite.com/sites/all/modules/lang_dropdown/msdropdown/jquery.dd.js?mqdxrf"></script>
<script type="text/javascript" src="http://mysite.com/sites/all/modules/lang_dropdown/lang_dropdown.js?mqdxrf"></script>
<script type="text/javascript" src="http://mysite.com/sites/all/modules/devel/devel_krumo_path.js?mqdxrf"></script>
share|improve this question
1  
The file is adding to the page but after drupal.js causing the error: That's not right - you're getting that error because your script is loading before jQuery, not because it's loading after drupal.js –  Clive Jul 23 at 11:34
 
Could you please show us order of your js files? Just turn off aggregation before you do that. –  Mołot Jul 23 at 11:36
 
@Mołot question updated –  user13134 Jul 23 at 11:55
 
You still didn't provide what I ask you, so I still cannot help. –  Mołot Jul 23 at 11:58
 
@Mołot having one of those days ;) –  user13134 Jul 23 at 11:58
show 1 more comments

2 Answers

In the end I got it working by changing the code to:

  drupal_add_js('misc/jquery.js', array(
    'group' => JS_LIBRARY,
    'weight' => -20,
    'every_page' => TRUE,
    'cache' => TRUE,
    'scope' => 'header',
  ));
  drupal_add_js('misc/jquery.once.js', array(
    'group' => JS_LIBRARY,
    'weight' => -19,
    'every_page' => TRUE,
    'cache' => TRUE,
    'scope' => 'header',
  ));

This then meet the rules on https://api.drupal.org/api/drupal/includes%21common.inc/function/drupal_add_js/7

  • First by scope, with 'header' first, 'footer' last, and any other scopes provided by a cust om theme coming in between, as determined by the theme.
  • Then by group.
  • Then by the 'every_page' flag, with TRUE coming before FALSE.
  • Then by weight.
  • Then by the order in which the JavaScript was added. For example, all else being the same, JavaScript added by a call to drupal_add_js() that happened later in the page request gets added to the page after one for which drupal_add_js() happened earlier in the page request.
share|improve this answer

You should not add jQuery manually. It is added by Drupal itself. Adding existing libraries is known to cause problems.

If you need newer jQuery version than one included within Drupal, use jQuery update module.

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.