Early Bird Registration for DrupalCon Portland 2024 is open! Register by 23:59 PST on 31 March 2024, to get $100 off your ticket.
By tce on
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
Try something like
Try something like this:
Thanks for your reply,
Thanks for your reply, unfortunately that produces this:
But I need this:
With drupal_add_js(), how do include the
api_key: your_api_key_goes_here
part?You should be able to do this
You should be able to do this with drupal_add_html_head(). I haven't done this specifically, but I've done similar things.
Thanks for your help. I
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.
Ahh, sorry, I missed that
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.
Thanks for your reply. I
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.
I just played around with
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!
I agree. Thanks for your
I agree. Thanks for your help.
Actually, you do have one
Actually, you do have one other option, but it involves overriding html.tpl.php in your theme.
Then in html.tpl.php, after $scripts and before </head>, you can add:
Thank you. It is always good
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.
You could also try adding it
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.
The problem I'm finding with
The problem I'm finding with adding it normally is I cannot add the additional variables inside the script tag to end up with:
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.
googleapis
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
Nice; that did the trick for
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.
Thanks @tce.