I'm trying to add some JS to the head, but I need it in this format:

<script type="text/javascript" src="http://platform.linkedin.com/in.js">
  api_key: your_api_key_goes_here
</script>

I can't figure out how to do this using drupal_add_js(). I have managed to do it using the drupal_add_html_head(), but I can't get it to appear below the title tag, even after changing the weight to something really high.

<?php
$output = array(
  '#type' => 'markup',
  '#markup' => '<script language="javascript" type="text/javascript" src="http://platform.linkedin.com/in.js">api_key: mykey</script>' . "\r",
  '#weight' => 10000,
);
drupal_add_html_head($output, 'linkedin_platform');
?>

Anyone know how I can add it, but at the end of the head?

Comments

Daglees’s picture

Try something like this:

 drupal_add_js('http://platform.linkedin.com/in.js',
    array('type' => 'external', 'scope' => 'header', 'weight' => 1000)
  );
tce’s picture

Thanks for your reply, unfortunately that produces this:

<script type="text/javascript" src="http://platform.linkedin.com/in.js"></script>

But I need this:

<script type="text/javascript" src="http://platform.linkedin.com/in.js">
  api_key: your_api_key_goes_here
</script>

With drupal_add_js(), how do include the api_key: your_api_key_goes_here part?

Jaypan’s picture

You should be able to do this with drupal_add_html_head(). I haven't done this specifically, but I've done similar things.

tce’s picture

Thanks for your reply. I mentioned previously that I have tried drupal_add_html_head(), but my problem is that it is appearing above the title tag, no matter what weight I set. I would like it to appear after all the other Javascript tags, at the end of the head.

Jaypan’s picture

Ahh, sorry, I missed that part of your comment.

Try using it anyways, and changing it's location in hook_page_alter() - that may allow it to render at the end of the head.

tce’s picture

Thanks for your reply. I could not seem to access the head information in hook_page_alter(), although I could in hook_html_head_alter() but unfortunately it still does not take me past the meta title tag. However, I did end up with this, which seems to work. Might not be the best way to do it though.

<?php
function dev_process_html(&$vars) {
  $output = array(
    '#type' => 'markup',
    '#markup' => '<script language="javascript" type="text/javascript" src="http://platform.linkedin.com/in.js">api_key: mykey</script>' . "\r",
  );
  $vars['scripts'] .= drupal_render($output);
}
?>
Jaypan’s picture

I just played around with this, and actually I think you have the best way to do it. The reason is that there are no variables outputted in html.tpl.php after the $scripts variable, which means that the only way to output something after the scripts would be to append it to the $scripts variable... which you have done!

tce’s picture

I agree. Thanks for your help.

Jaypan’s picture

Actually, you do have one other option, but it involves overriding html.tpl.php in your theme.

function my_module_preprocess_html(&$vars)
{
  $vars['additional_scripts'] = array(
    '#type' => 'markup',
    '#markup' => '<script language="javascript" type="text/javascript" src="http://platform.linkedin.com/in.js">api_key: mykey</script>' . "\r",
    '#weight' => 10000,
  );
}

Then in html.tpl.php, after $scripts and before </head>, you can add:

print render($additional_scripts);
tce’s picture

Thank you. It is always good to think about the other options. Though, for this project, I am doing this in a module so I would like to keep away from changing templates.

Jaypan’s picture

You could also try adding it normally, and setting it's position with hook_js_alter().

Just throwing out more options. Your current method should work fine.

tce’s picture

The problem I'm finding with adding it normally is I cannot add the additional variables inside the script tag to end up with:

<script type="text/javascript" src="http://platform.linkedin.com/in.js">
  api_key: your_api_key_goes_here
</script>

I can't see any option in drupal_add_js() for this. I don't know Javascript well enough to know how to make this work outside the script tag. I think the current method is suitable for my needs and I don't think it will interfere with any other modules.

brian.sanchez’s picture

this worked for me:

$google_map_sensor = array(
'#type' => 'markup',
'#markup' => '

' . "\r",
'#weight' => 10000,
);
drupal_add_html_head($google_map_sensor, 'google_map_sensor');

Best
Brian

_vid’s picture

Nice; that did the trick for me. I wanted my script as the last thing loaded in the head. I was using drupal_add_html_head in hook_process_html and couldn't get it to respect the #weight for '#type' => 'markup'; but this worked.

hook_process_html(&$vars) {
$element = array(
      '#type' => 'markup',
      '#markup' => $my_heredoc_css_js,
    );
  $vars['scripts'] .= drupal_render($element);
}

Thanks @tce.