Question

I want to load an external script in <head>. It is module specific so I want my module to take care of the loading.

In Drupal 6, function drupal_add_js() does not allow to add an external script in <head>. It will be available in Drupal 7 passing the "external" argument to the function. In D6, I can use drupal_set_html_head() instead, but it inserts the given data in the beginning of the <head> which I don't want. I'd like to append data in it.

It turned out that drupal_html_set_head() appends data.

$stored_head .= $data ."\n";

So the behavior I experimented--data was inserted in the beginning of head data--should come from that I call drupal_html_set_head() in my module's hook_init() function.

How can I append data to the very and of <head>?

Was it helpful?

Solution

The default page.tpl.php (you can find it in /modules/system/page.tpl.php is this:

<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>

When you make drupal_set_html_head() it is appending stuff to variable $head, but still there are more variables appended as you see.

One possible solutions is to append the stuff that you want to $scripts, instead of $head.

How?

with a preprocess function from your module:

function MYMODULE_preprocess_page(&$variables) {
    $variables['scripts'] .= $your_stuff;
}

I didn't try this solution, but if doesn't work maybe is because the order of execution. Try to set your module's weight higher, so it will run after the system_preprocess_page.

Other reason why may not work is because the theme is printing the variables in different order in page.tpl.php. But you can't control this from the code of a module.

OTHER TIPS

There are multiple solutions to work around the problem.

  • use hook_footer() to add the js to the footer
  • make a small jquery script, which creates the <script> element and adds it to <head>
  • do it with a template preprocess function
  • make your own page.tpl.php

Drupal uses the .= operator to create the $head variable. The $js and $css is kept in an array, and allows you to be re-orderd. $head is a string, and cannot be re-orderd, othern then by ugly, error-prone hacks. Such as regular-expression explosions, re-ordering and imploding.

$heads = preg_explode("\n", $head);
ksort($heads);
$head = implode("\n", $heads);

in your theme's variable pre-processor.

Did you see this comment?

Beware when using this to set an external js file call in D6, eg:

drupal_set_html_head('');

If you don't include a full script closing tag (ie ), it will break in Firefox (and possibly other browsers, webkit-based ones seem fine). The tag will fail to close, and will wipe out any other headers up to the next available tag. This usually results in all your css files failing to load, plus the next fully-tagged js file. So use this form instead:

drupal_set_html_head('');

This applies up to and including Drupal 6.16. You can overcome it in D7 with the new external option in drupal_add_js.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top