diff options
author | David Rothstein <drothstein@gmail.com> | 2015-03-30 22:28:39 -0400 |
---|---|---|
committer | David Rothstein <drothstein@gmail.com> | 2015-03-30 22:28:39 -0400 |
commit | f41ecaf25d7a38923e026ef45a74dffaf58f9479 (patch) | |
tree | 108e12ae7904c579809fcaabb31644389d23780a /includes/common.inc | |
parent | 63065623fba4087ba574b64efce054ae5a5b0683 (diff) | |
download | brdo-f41ecaf25d7a38923e026ef45a74dffaf58f9479.tar.gz brdo-f41ecaf25d7a38923e026ef45a74dffaf58f9479.tar.bz2 |
Issue #1279226 by attiks, ericduran, Wim Leers, sun, David_Rothstein, nod_: Allow sites and modules to skip loading jQuery and Drupal JavaScript libraries on pages where they won't be used
Diffstat (limited to 'includes/common.inc')
-rw-r--r-- | includes/common.inc | 40 |
1 files changed, 35 insertions, 5 deletions
diff --git a/includes/common.inc b/includes/common.inc index 0fac77201..0437ec1a4 100644 --- a/includes/common.inc +++ b/includes/common.inc @@ -4162,6 +4162,13 @@ function drupal_region_class($region) { * else being the same, JavaScript added by a call to drupal_add_js() that * happened later in the page request gets added to the page after one for * which drupal_add_js() happened earlier in the page request. + * - requires_jquery: Set this to FALSE if the JavaScript you are adding does + * not have a dependency on jQuery. Defaults to TRUE, except for JavaScript + * settings where it defaults to FALSE. This is used on sites that have the + * 'javascript_always_use_jquery' variable set to FALSE; on those sites, if + * all the JavaScript added to the page by drupal_add_js() does not have a + * dependency on jQuery, then for improved front-end performance Drupal + * will not add jQuery and related libraries and settings to the page. * - defer: If set to TRUE, the defer attribute is set on the <script> * tag. Defaults to FALSE. * - cache: If set to FALSE, the JavaScript file is loaded anew on every page @@ -4179,6 +4186,14 @@ function drupal_region_class($region) { */ function drupal_add_js($data = NULL, $options = NULL) { $javascript = &drupal_static(__FUNCTION__, array()); + $jquery_added = &drupal_static(__FUNCTION__ . ':jquery_added', FALSE); + + // If the $javascript variable has been reset with drupal_static_reset(), + // jQuery and related files will have been removed from the list, so set the + // variable back to FALSE to indicate they have not yet been added. + if (empty($javascript)) { + $jquery_added = FALSE; + } // Construct the options, taking the defaults into consideration. if (isset($options)) { @@ -4189,6 +4204,9 @@ function drupal_add_js($data = NULL, $options = NULL) { else { $options = array(); } + if (isset($options['type']) && $options['type'] == 'setting') { + $options += array('requires_jquery' => FALSE); + } $options += drupal_js_defaults($data); // Preprocess can only be set if caching is enabled. @@ -4199,14 +4217,18 @@ function drupal_add_js($data = NULL, $options = NULL) { $options['weight'] += count($javascript) / 1000; if (isset($data)) { - // Add jquery.js and drupal.js, as well as the basePath setting, the - // first time a JavaScript file is added. - if (empty($javascript)) { + // Add jquery.js, drupal.js, and related files and settings if they have + // not been added yet. However, if the 'javascript_always_use_jquery' + // variable is set to FALSE (indicating that the site does not want jQuery + // automatically added on all pages) then only add it if a file or setting + // that requires jQuery is being added also. + if (!$jquery_added && (variable_get('javascript_always_use_jquery', TRUE) || $options['requires_jquery'])) { + $jquery_added = TRUE; // url() generates the prefix using hook_url_outbound_alter(). Instead of // running the hook_url_outbound_alter() again here, extract the prefix // from url(). url('', array('prefix' => &$prefix)); - $javascript = array( + $default_javascript = array( 'settings' => array( 'data' => array( array('basePath' => base_path()), @@ -4225,11 +4247,13 @@ function drupal_add_js($data = NULL, $options = NULL) { 'group' => JS_LIBRARY, 'every_page' => TRUE, 'weight' => -1, + 'requires_jquery' => TRUE, 'preprocess' => TRUE, 'cache' => TRUE, 'defer' => FALSE, ), ); + $javascript = drupal_array_merge_deep($javascript, $default_javascript); // Register all required libraries. drupal_add_library('system', 'jquery', TRUE); drupal_add_library('system', 'jquery.once', TRUE); @@ -4270,6 +4294,7 @@ function drupal_js_defaults($data = NULL) { 'group' => JS_DEFAULT, 'every_page' => FALSE, 'weight' => 0, + 'requires_jquery' => TRUE, 'scope' => 'header', 'cache' => TRUE, 'defer' => FALSE, @@ -4316,7 +4341,12 @@ function drupal_get_js($scope = 'header', $javascript = NULL, $skip_alter = FALS if (!isset($javascript)) { $javascript = drupal_add_js(); } - if (empty($javascript)) { + + // If no JavaScript items have been added, or if the only JavaScript items + // that have been added are JavaScript settings (which don't do anything + // without any JavaScript code to use them), then no JavaScript code should + // be added to the page. + if (empty($javascript) || (isset($javascript['settings']) && count($javascript) == 1)) { return ''; } |