2

My site is built on Drupal 7, and a number of my .js scripts are throwing this exception: Uncaught TypeError: Property '$' of object [object Window] is not a function. After some research, I think this is related to the new jQuery namespace update, but since we didn't write the scripts, I'd rather remove them than make changes.

I've traced the references to the $scripts variable in html.tpl.php, but haven't been able to trace back any farther. I've examined .info files for additions to scripts[], drupal_add_js(), drupal_add_library(), preprocess and process function calls, and found nothing.

Has anyone had similar issues with removing JS file references, and could you give me a rundown on Drupal's script inclusion process? I found the methods above digging through docs and help sites, but I'm still not clear on how it all comes together (i.e. if scripts[] variable additions are added before drupal_add_js() calls, etc.).

  • Have you add any custom js file? You should use jQuery instad of $. jQuery(document).ready(function() { jQuery('#some').hide(); }); – Tyler Durden Aug 16 '12 at 20:17
  • Firebug or Webkit Web Inspector show file name and line number for code with error. You should disable JS aggregation first. – kalabro Aug 16 '12 at 20:17
  • Thanks, but I'm more interested in removing the scripts than fixing them. For example. my site uses the Disqus module, which uses one of these problematic scripts. I'd rather not edit the script directly, since it could potentially create maintenance risks further down the road. – thinkTech Aug 16 '12 at 21:04
0

if scripts[] variable additions are added before drupal_add_js() calls, etc

The JavaScript files used by a theme, and defined with scripts[] directives are added in _drupal_theme_initialize(), using the following code.

  // Do basically the same as the above for scripts
  $final_scripts = array();

  // Grab scripts from base theme
  foreach ($base_theme as $base) {
    if (!empty($base->scripts)) {
      foreach ($base->scripts as $name => $script) {
        $final_scripts[$name] = $script;
      }
    }
  }

  // Add scripts used by this theme.
  if (!empty($theme->scripts)) {
    foreach ($theme->scripts as $name => $script) {
      $final_scripts[$name] = $script;
    }
  }

  // Add scripts used by this theme.
  foreach ($final_scripts as $script) {
    drupal_add_js($script, array('group' => JS_THEME, 'every_page' => TRUE));
  }

_drupal_theme_initialize() is indirectly called from theme(), l(), contextual_preprocess(), and ThemeRegistry::initializeRegistry().

As each JavaScript file added to a page has its own group, with its own weight, it doesn't generally have importance which file is added first. If you really need to be sure a file is included before another one, you should define its group, and its weight, consider that files are included following this order:

  • First by scope, with 'header' first, 'footer' last, and any other scopes provided by a custom 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

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.