diff --git a/core/includes/common.inc b/core/includes/common.inc index 14154af..ef2c0d7 100644 --- a/core/includes/common.inc +++ b/core/includes/common.inc @@ -4035,6 +4035,8 @@ function drupal_region_class($region) { * which drupal_add_js() happened earlier in the page request. * - defer: If set to TRUE, the defer attribute is set on the <script> * tag. Defaults to FALSE. + * - async: If set to TRUE, the async attribute is set on the <script> + * tag. Defaults to FALSE. * - cache: If set to FALSE, the JavaScript file is loaded anew on every page * call; in other words, it is not cached. Used only when 'type' references * a JavaScript file. Defaults to TRUE. @@ -4107,6 +4109,7 @@ function drupal_add_js($data = NULL, $options = NULL) { 'preprocess' => TRUE, 'cache' => TRUE, 'defer' => FALSE, + 'async' => FALSE, 'browsers' => array(), ), ); @@ -4154,6 +4157,7 @@ function drupal_js_defaults($data = NULL) { 'scope' => 'header', 'cache' => TRUE, 'defer' => FALSE, + 'async' => FALSE, 'preprocess' => TRUE, 'version' => NULL, 'data' => $data, @@ -4321,9 +4325,6 @@ function drupal_pre_render_scripts($elements) { foreach ($group['items'] as $item) { // Element properties that do not depend on item type. $element = $element_defaults; - if (!empty($item['defer'])) { - $element['#attributes']['defer'] = 'defer'; - } $element['#browsers'] = $item['browsers']; // Element properties that depend on item type. @@ -4351,6 +4352,18 @@ function drupal_pre_render_scripts($elements) { break; } + // The defer and async attributes must not be specified if the src + // attribute is not present. + if (!empty($element['#attributes']['src'])) { + // Both may be specified for legacy browser fallback purposes. + if (!empty($item['async'])) { + $element['#attributes']['async'] = 'async'; + } + if (!empty($item['defer'])) { + $element['#attributes']['defer'] = 'defer'; + } + } + $elements[] = $element; } }