简体   繁体   English

在drupal页面或表单中包含javascript

[英]Including javascript in a drupal page or form

Is there a drupal function or convention for including JavaScript onto a page or form? 是否存在将JavaScript包含到页面或表单中的drupal函数或约定?

My current solution is to hard code the script tag as part of the output of my form's theming function, but it seems like there should be a better way, since JavaScript should generally be the last thing loaded on the page. 我当前的解决方案是将脚本标记作为表单主题功能输出的一部分进行硬编码,但是似乎应该有一种更好的方法,因为JavaScript通常应该是页面上加载的最后一件事。

The function you are looking for is called drupal_add_js() . 您要查找的函数称为drupal_add_js() It lets you control several aspects of adding js to pages. 它使您可以控制将js添加到页面的几个方面。

Copied and pasted from the link provided above, these are the parameters of the function: 从上面提供的链接复制和粘贴的这些是函数的参数:

$data (optional) If given, the value depends on the $type parameter: * 'core', 'module' or 'theme': Path to the file relative to base_path(). $ data (可选),如果给定,则值取决于$ type参数:*'core','module'或'theme':相对于base_path()的文件路径。 * 'inline': The JavaScript code that should be placed in the given scope. *'inline':应该放在给定范围内的JavaScript代码。 * 'setting': An array with configuration options as associative array. *'setting':具有配置选项的数组作为关联数组。 The array is directly placed in Drupal.settings. 该数组直接放置在Drupal.settings中。 You might want to wrap your actual configuration settings in another variable to prevent the pollution of the Drupal.settings namespace. 您可能希望将实际配置设置包装在另一个变量中,以防止污染Drupal.settings命名空间。

$type (optional) The type of JavaScript that should be added to the page. $ type (可选)应添加到页面的JavaScript的类型。 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. 它被视为对JavaScript文件的引用。 Defaults to 'module'. 默认为“模块”。

$scope (optional) The location in which you want to place the script. $ scope (可选)要放置脚本的位置。 Possible values are 'header' and 'footer' by default. 默认情况下,可能的值为“ header”和“ footer”。 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 tag. $ defer (可选)如果设置为TRUE,则在标记上设置defer属性。 Defaults to FALSE. 默认为FALSE。 This parameter is not used with $type == 'setting'. $ type =='setting'不使用该参数。

$cache (optional) If set to FALSE, the JavaScript file is loaded anew on every page call, that means, it is not cached. $ cache (可选)如果设置为FALSE,则在每次页面调用时都会重新加载JavaScript文件,这意味着不会对其进行缓存。 Defaults to TRUE. 默认为TRUE。 Used only when $type references a JavaScript file. 仅在$ type引用JavaScript文件时使用。

$preprocess (optional) Should this JS file be aggregated if this feature has been turned on under the performance section? $ preprocess (可选)如果已在“性能”部分下启用了此功能,是否应该聚合此JS文件?

Hope this helps! 希望这可以帮助!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM