From ec0dbd35c8dd7ad646ea3880f52ad81a54188f51 Mon Sep 17 00:00:00 2001 From: Angie Byron Date: Sat, 28 Feb 2009 07:36:06 +0000 Subject: #91250 by ontwerpwerk, hass, profix898, Rob Loach, and mfer: Allow drupal_add_js() to reference external JavaScript files. --- includes/common.inc | 25 +++++++++++++++++++------ 1 file changed, 19 insertions(+), 6 deletions(-) (limited to 'includes') diff --git a/includes/common.inc b/includes/common.inc index 2024a25c9..2a9dd2cbf 100644 --- a/includes/common.inc +++ b/includes/common.inc @@ -2337,6 +2337,11 @@ function drupal_clear_css_cache() { * When adding inline code, make sure that you are not relying on $ being jQuery. * Wrap your code in (function($) { ... })(jQuery); or use jQuery instead of $. * + * - Add external JavaScript ('external'): + * Allows the inclusion of external JavaScript files that are not hosted on the + * local server. Note that these external JavaScript references do not get + * aggregated when preprocessing is on. + * * - Add settings ('setting'): * Adds a setting to Drupal's global storage of JavaScript settings. Per-page * settings are required by some modules to function properly. All settings @@ -2350,12 +2355,14 @@ function drupal_clear_css_cache() { * drupal_add_js('jQuery(document).ready(function(){alert("Hello!");});', * array('type' => 'inline', 'scope' => 'footer', 'weight' => 5) * ); + * drupal_add_js('http://example.com/example.js', 'external'); * @endcode * * @param $data * (optional) If given, the value depends on the $options parameter: * - 'file': Path to the file relative to base_path(). * - 'inline': The JavaScript code that should be placed in the given scope. + * - 'external': The absolute path to a JavaScript file hosted externally. * - 'setting': An array with configuration options as associative array. The * array is directly placed in Drupal.settings. All modules should wrap * their actual configuration settings in another variable to prevent @@ -2369,8 +2376,8 @@ function drupal_clear_css_cache() { * always pass the string 'setting' only. * - type * The type of JavaScript that is to be added to the page. Allowed - * values are 'file', 'inline', 'setting', or 'reset'. Defaults to - * 'file'. Note that if type is 'reset', then $data and all other + * values are 'file', 'inline', 'external', 'setting', or 'reset'. Defaults + * to 'file'. Note that if type is 'reset', then $data and all other * $options will be ignored and the JavaScript added so far will be * reset. * - scope @@ -2404,7 +2411,8 @@ function drupal_clear_css_cache() { * a JavaScript file. Defaults to TRUE. * - preprocess * Aggregate the JavaScript if the JavaScript optimization setting has - * been toggled in admin/settings/performance. Defaults to TRUE. + * been toggled in admin/settings/performance. Note that JavaScript of + * type 'external' is not aggregated. Defaults to TRUE. * @return * The contructed array of JavaScript files. * @see drupal_get_js() @@ -2476,9 +2484,9 @@ function drupal_add_js($data = NULL, $options = NULL) { $javascript[] = $options; break; - case 'file': - // Files must keep their name as the associative key so the same - // JavaScript files can not be added twice. + default: // 'file' and 'external' + // Local and external files must keep their name as the associative key + // so the same JavaScript file is not be added twice. $javascript[$options['data']] = $options; break; } @@ -2596,6 +2604,11 @@ function drupal_get_js($scope = 'header', $javascript = NULL) { $files[$item['data']] = $item; } break; + + case 'external': + // Preprocessing for external JavaScript files is ignored. + $output .= '\n"; + break; } } -- cgit v1.2.3