我想在 <head>
中加载一个外部脚本.它是特定于模块的,所以我希望我的模块负责加载。
在 Drupal 6 中,函数 drupal_add_js()
不允许在 <head>
中添加外部脚本.它将在 Drupal 7 中提供通过 "external"
函数的参数。 在 D6 中,我可以使用 drupal_set_html_head()
相反,它会将给定的数据插入到 <head>
的开头我不想要。 我想在其中附加数据。
原来是drupal_html_set_head()
追加数据。
$stored_head .= $data ."\n";
所以我实验的行为——数据被插入到头部数据的开头——应该来 self 称之为 drupal_html_set_head()
的地方在我模块的 hook_init()
功能。
如何将数据附加到 <head>
的非常和?
最佳答案
默认的page.tpl.php(你可以在/modules/system/page.tpl.php中找到它是这样的:
<head>
<title><?php print $head_title; ?></title>
<?php print $head; ?>
<?php print $styles; ?>
<?php print $scripts; ?>
<script type="text/javascript"><?php /* Needed to avoid Flash of Unstyled Content in IE */ ?> </script>
</head>
当您创建 drupal_set_html_head()
时,它会将内容附加到变量 $head
,但如您所见,仍然附加了更多变量。
一个可能的解决方案是将您想要的内容附加到 $scripts
,而不是 $head
。
如何?
使用模块中的预处理函数:
function MYMODULE_preprocess_page(&$variables) {
$variables['scripts'] .= $your_stuff;
}
我没有尝试过这个解决方案,但如果不起作用可能是因为执行顺序。尝试将模块的权重设置得更高,这样它将在 system_preprocess_page
之后运行。
可能不起作用的其他原因是因为主题在 page.tpl.php 中以不同的顺序打印变量。但是您无法从模块的代码中控制它。
关于drupal - 在 Drupal 6 的 <head> 中加载外部脚本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3668474/