添加 JavaScript 文件
我尝试使用 drupal_add_js()
在标头中插入对 Javascript 文件的引用。我将这一行放在 template.php 的模板预处理函数中。结果是代码根本不起作用:输出中没有应有的脚本链接。谁能告诉我我做错了什么?
function phptemplate_preprocess_page(&$vars) {
$url = drupal_get_path("theme","mysite");
drupal_add_js($url."/jquery.js");
drupal_add_js($url."/drupal.js");
.....
I'm trying to insert reference to the Javascript file in the header by using drupal_add_js()
. I placed this line inside the template preprocess function in template.php. The result that the code is not working at all: There is no script link in output as it should be. Can anyone tell me what am I doing wrong?
function phptemplate_preprocess_page(&$vars) {
$url = drupal_get_path("theme","mysite");
drupal_add_js($url."/jquery.js");
drupal_add_js($url."/drupal.js");
.....
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
更简单的是,需要在所有页面上加载的Javascript可以添加到主题的.info文件中。请参阅http://drupal.org/node/171205#scripts。
Even easier, Javascript that needs to be loaded on all pages can be added in the theme's .info file. See http://drupal.org/node/171205#scripts.
如果您将 javascript 文件放在主题目录中,则只需将以下内容添加到主题 .info 文件中。
添加此文件后,您需要停用主题,然后重新激活它。
If you place the javascript file in the theme directory, you can just add the following to the themes .info file
After you add this file you need to deactivate your theme and then reactive it.
正如其他人所指出的,仅使用
hook_preprocess_page()
实现中的drupal_add_js()
是行不通的。通过多次调用drupal_add_js()
收集的 JavaScript 文件引用用于将相应的标记生成到template_preprocess_page()
。但主题的hook_preprocess_page()
实现始终在template_preprocess_page()
之后调用。因此,为了通过drupal_add_js()
添加文件到您的.tpl.php
文件中,您需要覆盖已经设置的$scripts< /code> 变量:
但是,您不必自己添加
jquery.js
和drupal.js
,它应该已经由 Drupal 核心自动完成。如果您需要自己执行此操作,那么您的网站上就会出现某些问题。您可以(重新)添加文件作为快速修复,但您最好找到问题的根本原因,因为它很可能会产生您尚未识别(或在没有意识到的情况下解决)的其他问题。As pointed by other, simply using
drupal_add_js()
from ahook_preprocess_page()
implementation doesn't work. The references to JavaScript files collected through the multiple calls todrupal_add_js()
are used to generate the corresponding markup into the$scripts
variables fromtemplate_preprocess_page()
. But a theme's implementation ofhook_preprocess_page()
is always called aftertemplate_preprocess_page()
. So in order to have the files added throughdrupal_add_js()
in your.tpl.php
file(s), you need to override the already set$scripts
variables:But, you shouldn't have to add
jquery.js
anddrupal.js
yourself, it should already be done automatically by Drupal core. If you need to do it yourself, then something is broken on your site. You can (re-)add the files as a quick fix, but you better find the root cause of the issue as it is most likely creating other issues you haven't yet identified (or worked around without realizing it).drupal_add_js()
可以工作,但是您将其深入到页面渲染过程中。我建议你像你正在做的那样将它放在 template.php 中,但在一开始,在任何函数之外。这就是我们在一些项目中所做的。drupal_add_js()
works, but you are putting it deep into the page rendering process. I suggest you put it in the template.php like you are doing, but in the beginning, outside any function. This is what we did on a few of our projects.