call a js function on loading a page
If you want to add arbitrary code to the &lt;head&gt; element you need to:


Take a copy of the core html.tpl.php file and put it in your theme somewhere
Clear the caches so it gets added to the theme registry
Implement hook_preprocess_html() in your theme's template.php file like so

function MYTHEME_preprocess_html(&amp;$vars) {
  $vars['some_var'] = 'Something';
}

In the html.tpl.php file, use

print $some_var;


wherever that might be appropriate.


If you want to add structured code to the &lt;head&gt; tag then use drupal_add_html_head().

Finally, specifically to add JS to the page, you can use drupal_add_js(). But that needs to happen before the HTML template is processed. hook_preprocess_page() would be one option, but there are many, depending on the context that makes up your page.

You'll find the Theming guide, and questions already asked on this site, invaluable if you're learning the theme system from scratch.