drupal_add_js
includes/common.inc, строка 1736
- Версии
- 5
drupal_add_js($data = NULL, $type = 'module', $scope = 'header', $defer = FALSE, $cache = TRUE)
- 6
drupal_add_js($data = NULL, $type = 'module', $scope = 'header', $defer = FALSE, $cache = TRUE, $preprocess = TRUE)
Добавляет JavaScript файл, настройки или код на страницу.
Действие этой функции меняется, в зависимости от параметров, с которыми она вызывается. В основном она служит для добавления кода JavaScript на страницу, как в виде ссылки на существующий файл или как участок кода прямо на странице. С помощью этой функции можно осуществить следующие действия:
- 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.
Параметры
$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.
$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'
.
$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.
$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'
.
$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.
Возвращаемое значение
If the first parameter is NULL
, the JavaScript array that has been built so
far for $scope
is returned.
Связанные темы
Код
<?php
function drupal_add_js($data = NULL, $type = 'module', $scope = 'header', $defer = FALSE, $cache = TRUE) {
if (!is_null($data)) {
_drupal_add_js('misc/jquery.js', 'core', 'header', FALSE, $cache);
_drupal_add_js('misc/drupal.js', 'core', 'header', FALSE, $cache);
}
return _drupal_add_js($data, $type, $scope, $defer, $cache);
}
?>
Войдите или зарегистрируйтесь, чтобы получить возможность отправлять комментарии