function 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()

Note: this list is generated by pattern matching, so it may include some functions that are not actually implementations of this hook.

locale_js_alter in modules/locale/locale.module
Implements hook_js_alter().
simpletest_js_alter in modules/simpletest/simpletest.module
Implements hook_js_alter().
1 invocation of hook_js_alter()
drupal_get_js in includes/common.inc
Returns a themed presentation of all JavaScript code for the current page.

File

modules/system/system.api.php, line 743
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

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;

This works for me:

function my_module_js_alter(&$javascript) {
    global $base_url;
    $jQuery_version = '1.7.2';
    //$jQuery_local = $base_url.'/'.drupal_get_path('module', 'my_module').'/jquery-1.7.2.min.js';
    $jQuery_local = $base_url.'/'.drupal_get_path('module', 'my_module').'/jquery-1.7.2.min.js?v='.$jQuery_version;
    $jQuery_cdn = 'http://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js';

    $javascript['misc/jquery.js']['data']    = $jQuery_cdn;
    $javascript['misc/jquery.js']['version'] = $jQuery_version;

    $group  = $javascript['misc/jquery.js']['group']  = JS_LIBRARY;
    $weight = $javascript['misc/jquery.js']['weight'] = -20;
   
    drupal_add_js('window.jQuery || document.write(\'<script type="text/javascript" src="'.$jQuery_local.'"><\/script>\')',
        array('type'=>'inline', 'scope'=>'header', 'group'=>$group, 'every_page'=>TRUE, 'weight'=>$weight));
}

Thk for posting. I saw very examples and not work but your code yes !!!
See you

Thanks... I test method 3 from http://drupal.org/node/1058168 but not works.. al least for me.

Hi,

I am using below function. But it's not taking preprocess function whether true or false, 1 or 0, Please suggest how to set it.
$javascript['modules/field/modules/text/text.js']['preprocess '] = FALSE;
Getting below error:
Notice: Undefined index: preprocess in drupal_get_js() (line 4351 of C:\xamp\xampplite\htdocs\drupal\includes\common.inc).

Code is

function drupalmodule_js_alter(&$javascript) {
$javascript['modules/field/modules/text/text.js']['type'] = 'file';
$javascript['modules/field/modules/text/text.js']['scope'] = 'header';
$javascript['modules/field/modules/text/text.js']['every_page'] = TRUE;
$javascript['modules/field/modules/text/text.js']['weight'] = -20;
$javascript['modules/field/modules/text/text.js']['group'] = 0;
$javascript['modules/field/modules/text/text.js']['preprocess '] = FALSE;
$javascript['modules/field/modules/text/text.js']['defer'] = '';
$javascript['modules/field/modules/text/text.js']['cache'] = '';
$javascript['modules/field/modules/text/text.js']['data'] = drupal_get_path('module', 'drupalmodule') . '/drupalmodule.js';
}