hook_js_alter

7 system.api.php hook_js_alter(&$javascript)
8 system.api.php hook_js_alter(&$javascript)

Perform necessary alterations to the JavaScript before it is presented on the page.

Parameters

$javascript: An array of all JavaScript being presented on the page.

See also

drupal_add_js()

drupal_get_js()

drupal_js_defaults()

Related topics

2 functions implement hook_js_alter()

1 invocation of hook_js_alter()

File

modules/system/system.api.php, line 703
Hooks provided by Drupal core and the System module.

Code

function hook_js_alter(&$javascript) {
  // Swap out jQuery to use an updated version of the library.
  $javascript['misc/jquery.js']['data'] = drupal_get_path('module', 'jquery_update') . '/jquery.js';
}

Comments

Can this be used to put raw

Can this be used to put raw javascript contents into the JS that will be output.
The reason I ask is that if there are multiple $(document).ready() calls, the order of operations is only as predictable as the order in which JS files that contain this call are loaded. Some JS junkies have said that if you have to have more than one $(document).ready() call, we might be using the jQuery library incorrectly.

I believe wrapping your js in

I believe wrapping your js in Drupal Behaviors avoids the need for multiple document ready()'s. See http://drupal.org/node/756722

Out of scope error

When following the example above my logs were filling with drupal_get_js out of scope errors. Following the commit here on adaptive theme helped me fix.

http://drupalcode.org/project/adaptivetheme.git/blob_plain/b4acfa587348b...

Works

$file = drupal_get_path('theme', 'pp') . '/js/autocomplete.js';
$javascript['misc/autocomplete.js'] = drupal_js_defaults($file);
Errors

$file = drupal_get_path('theme', 'pp') . '/js/autocomplete.js';
$javascript['misc/autocomplete.js']['data'] =$file;

Login or register to post comments