2

I want to add a call for 2 external scripts in the page.tpl.php.

The scripts are:

 <script type="text/javascript" src="http://use.typekit.com/12345.js"></script>
 <script type="text/javascript">try{Typekit.load();}catch(e){}</script>

Now in D7 you can call them directly from page.tpl.php like

drupal_add_js()

right?

But I'm working on a D6 site right now.

So within mytheme directory I add to my template.php file

function mytheme_preprocess_page(&$vars, $hook) {
    drupal_set_html_head('<script type="text/javascript" src="http://use.typekit.com/12345.js"></script>');
    $vars['head'] = drupal_get_html_head();
  }

It doesn't work. The page no longer loads.

So the question is, How do I get both those scripts in the head? What should the code look like? I know the real answer is learn php, but in the meantime

1 Answer 1

5

You should concatenate $vars['head'] no replacing it: Your code should be:

function mytheme_preprocess_page(&$vars, $hook) {
    drupal_set_html_head('<script type="text/javascript" src="http://use.typekit.com/12345.js"></script>');
    $vars['head'] .= drupal_get_html_head();
}

Notice the .= after $vars['head'].

7
  • Thanks, that works great for one script. Now how do I add another script? add a whole new function? This doesn't work - pastie.org/2183883 Jul 8, 2011 at 17:21
  • Have you tried to put twice the drupal_set_html_head before getting it ? pastie.org/2184904
    – tostinni
    Jul 8, 2011 at 21:11
  • Another amateur php question - Is it okay that there are two - function service_preprocess_page(&$vars, $hook) { within the template.php file? Jul 11, 2011 at 16:43
  • Have you tried ?
    – tostinni
    Jul 11, 2011 at 17:18
  • Yeah, it was a no go, but I tried this too and that didn't work either - pastie.org/2197838 Jul 11, 2011 at 18:08

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.