drupal - 在 Drupal 6 的 <head> 中加载外部脚本

标签 drupal drupal-6 external-script html-head

我想在 <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/

相关文章:

drupal - 如何使用 Highcharts Drupal 7 模块

jquery - Drupal 行为

javascript - 有条件地加载外部脚本不起作用,没有错误

Javascript:有些东西是否太简单而无法链接到外部?

drupal 6 或 7 与 ubercart

ajax - SELECT2 ajax - 预加载选项

php - 在drupal中查找任何节点的字段值的最短代码

php - 在 Drupal 中重新通知用户帐户

drupal - 向 Drupal 添加另一个节点显示(如预告片、完整节点)