How to add js code in the head using a function? For a single page
Please consult function drupal_add_js 


  $options: (optional) A string defining the type of JavaScript that is being added in the $data parameter
  ('file'/'setting'/'inline'/'external'), or an associative array.
  JavaScript settings should always pass the string 'setting' only.
  Other types can have the following elements in the array:
  
  type: The type of JavaScript that is to be added to the page. Allowed values are 'file', 'inline', 'external' or 'setting'. Defaults
  to 'file'.
  scope: The location in which you want to place the script. Possible values are 'header' or 'footer'. If your theme implements
  different regions, you can also use these. Defaults to 'header'.


Note scope option in the text above.

Consult this post: Load JS &amp; CSS Files on Specific Pages in Drupal 7 for direction on using a template preprocess function wherein to add the js.