If you want to make sure you're javascript is loaded in the header:
For drupal 7
/sites/default/themes/yourtheme/template.php
function theme_preprocess_html(&$var) {
$path = $GLOBALS['base_url'] . '/' . path_to_theme() . '/js/yourscript-min.js';
$script = array(
'#tag' => 'script',
'#attributes' => array(
'type' => 'text/javascript',
'src' => $path
),
'#value' => '',
);
drupal_add_html_head($script, 'script');
}
or if you want it loaded in the page:
function theme_preprocess_html(&$var) {
$script = array(
'#tag' => 'script',
'#attributes' => array('type' => 'text/javascript'),
'#value' => 'Add your javascript here',
);
drupal_add_html_head($script, 'script');
}
For drupal 8
In the yourtheme.libraries.yml you need to create a library like so:
yourtheme.libraries.yml
library-name:
header: true
js:
js/yourSrctipForTheHeader.js: {}
Also in the theme.info.yml
you need to add this library, then it will automatically comes in head tag and will be available before the page load, if you remove header:true
the script will be loaded after the page load.
theme.info.yml
libraries:
- yourtheme/library-name
Alternativly, you can make use of this module:
https://www.drupal.org/project/header_and_footer_scripts and put the scipts in manually in the position that you want. But to be honest, i think the first solution is the more elegant way.