You are here

function async_js_add_js in Asynchronous JavaScript 8

Same name and namespace in other branches
  1. 7 async_js.module \async_js_add_js()

Queue scripts to be loaded asynchronously.

Parameters

string $src: The path to the file relative to base_path().

array $options: An associative array of options for each script. The following elements affect asynchronous loading functionality:

  • 'type': The type of JavaScript that is to be added to the page. Allowed values are 'file' or 'external'. Defaults to 'file'.
  • 'async_dependencies': An indexed array of scripts which must be loaded before the current script. The value of each item in the array must correspond exactly to the $src parameter originally used to load the dependency.
  • 'async_callback': The name of a javascript function, or an array of them, in the global scope which will be run once this particular script is finished loading.
  • 'async_fade': An array of jQuery selectors which correspond to elements on the page to fadeIn after a specified timeout and in unison. The fadeIn effect will be applied universally to all elements defined in this way on a single page.
  • 'async_container': A jQuery selector which corresponds to the element on the page in which the script should be inserted. If left blank, the script will be inserted before the first script element in the document.
  • 'cache': If set to FALSE, the JavaScript file is loaded anew on every asynchronous request. Defaults to TRUE.

See also

async_js_js_alter()

1 call to async_js_add_js()
async_js_js_alter in ./async_js.module
Implements hook_js_alter().

File

./async_js.module, line 37
Queue JavaScript files to be loaded asynchronously.

Code

function async_js_add_js($src = NULL, array $options = array()) {
  $javascript =& drupal_static(__FUNCTION__, array());
  if (!empty($src)) {

    // Set default options.
    $options += array(
      'type' => 'file',
      'async_dependencies' => array(),
      'async_callback' => array(),
      'async_fade' => array(),
      'async_container' => NULL,
      'cache' => TRUE,
    );

    // Add script to queue.
    $javascript[$src] = array(
      'data' => $src,
    ) + $options;
  }
  return $javascript;
}