There are various ways of doing this... and the Drupal API lists the following:
drupal_add_js('misc/collapse.js'); drupal_add_js('misc/collapse.js', 'file'); drupal_add_js('jQuery(document).ready(function () { alert("Hello!"); });', 'inline'); drupal_add_js('jQuery(document).ready(function () { alert("Hello!"); });', array( 'type' => 'inline' , 'scope' => 'footer' , 'weight' => 5 ) ); drupal_add_js('http://example.com/example.js', 'external'); drupal_add_js(array('myModule' => array('key' => 'value')), 'setting');
If Javascript is need on all pages, then you can either add it in the .info file:
scripts[] = js/script.js scripts[] = js/otherscript.js
It can also be done in the template.php file too.... as suggested by hdcwebteam
function MYTHEME_preprocess_page(&$variables) { drupal_add_js(drupal_get_path('theme', 'MYTHEME') .'/mytheme.js', 'file'); }
...or if adding script like CrazyEgg...
function MYTHEME_preprocess_page(&$variables) { $crazyEggScript = 'PASTE SCRIPT IN HERE'; drupal_add_js($crazyEggScript, array( 'type' => 'inline' , 'scope' => 'footer' , 'weight' => 5 ) ); }
Main Category
Secondary Categories