欢迎各位兄弟 发布技术文章

这里的技术是共享的

You are here

drupal_add_js 异步加载 js 有大用 有大大用

见  drupal_add_js  的定义  ( \includes\common.inc 这个文件里面)


/**
* Add a JavaScript file, setting or inline code to the page.
*
* The behavior of this function depends on the parameters it is called with.
* Generally, it handles the addition of JavaScript to the page, either as
* reference to an existing file or as inline code. The following actions can be
* performed using this function:
*
* - Add a file ('core', 'module' and 'theme'):
*   Adds a reference to a JavaScript file to the page. JavaScript files
*   are placed in a certain order, from 'core' first, to 'module' and finally
*   'theme' so that files, that are added later, can override previously added
*   files with ease.
*
* - Add inline JavaScript code ('inline'):
*   Executes a piece of JavaScript code on the current page by placing the code
*   directly in the page. This can, for example, be useful to tell the user that
*   a new message arrived, by opening a pop up, alert box etc.
*
* - 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. The settings
*   will be accessible at Drupal.settings.
*
* @param $data
*   (optional) If given, the value depends on the $type parameter:
*   - 'core', 'module' or 'theme': Path to the file relative to base_path().
*   - 'inline': The JavaScript code that should be placed in the given scope.
*   - 'setting': An array with configuration options as associative array. The
*       array is directly placed in Drupal.settings. You might want to wrap your
*       actual configuration settings in another variable to prevent the pollution
*       of the Drupal.settings namespace.
* @param $type
*   (optional) The type of JavaScript that should be added to the page. Allowed
*   values are 'core', 'module', 'theme', 'inline' and 'setting'. You
*   can, however, specify any value. It is treated as a reference to a JavaScript
*   file. Defaults to 'module'.
* @param $scope
*   (optional) The location in which you want to place the script. Possible
*   values are 'header' and 'footer' by default. If your theme implements
*   different locations, however, you can also use these.
* @param $defer
*   (optional) If set to TRUE, the defer attribute is set on the <script> tag.
*   Defaults to FALSE. This parameter is not used with $type == 'setting'.
* @param $cache
*   (optional) If set to FALSE, the JavaScript file is loaded anew on every page
*   call, that means, it is not cached. Defaults to TRUE. Used only when $type
*   references a JavaScript file.
* @param $preprocess
*   (optional) Should this JS file be aggregated if this
*   feature has been turned on under the performance section?
* @return
*   If the first parameter is NULL, the JavaScript array that has been built so
*   far for $scope is returned. If the first three parameters are NULL,
*   an array with all scopes is returned.
*/
function drupal_add_js($data = NULL, $type = 'module', $scope = 'header', $defer = FALSE, $cache = TRUE, $preprocess = TRUE) {



本人的用法  自己亲自做的  有大用

if($arg0=='listkefu'){
  drupal_add_js(drupal_get_path('theme', 'myminnellizhutwo') . '/js/listkefu/ajax_xgz_and_yywgz_num.js','theme','header',true);
  $vars['scripts'] = drupal_get_js();
}


普通分类: