0

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>
  • 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 '13 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 '13 at 11:36
  • @Mołot question updated – user13134 Jul 23 '13 at 11:55
  • You still didn't provide what I ask you, so I still cannot help. – Mołot Jul 23 '13 at 11:58
  • @Mołot having one of those days ;) – user13134 Jul 23 '13 at 11:58
3

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.
2

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.

  • But jQuery Update only allows to update to 1.8, any shamely, anything higher than jQuery 1.8 will cause Drupal to not work anymore...lol – Solomon Closson Jan 2 '14 at 0:12
  • @SolomonClosson yes, anything higer than jQuery 1.8 will make Drupal fail as they gave up on backward compatibility. That's the very reason jQuery Update will not allow you to go higher. But dev branch allows you to go up to 1.10.2 and use separate version on admin pages, so if your frontside theme replaces all jQuery version-sensitive scripts provided by core with appropriate hooks and theming functions, then you are OK. and if it does not, changing jQuery manually is even more of a disaster. – Mołot Jan 2 '14 at 7:37
  • Appropriate hooks and theming functions in your theme, isn't that what jQuery Update should do according to your description of it in the dev build? – Solomon Closson Jan 2 '14 at 23:53

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.