How is JavaScript included in Drupal 7? 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.).
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-&gt;scripts)) {
      foreach ($base-&gt;scripts as $name =&gt; $script) {
        $final_scripts[$name] = $script;
      }
    }
  }

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

  // Add scripts used by this theme.
  foreach ($final_scripts as $script) {
    drupal_add_js($script, array('group' =&gt; JS_THEME, 'every_page' =&gt; 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