What hook should I use to attach a script to a content type? I have this function from a standalone php script that redirects the user based off the user agent. In the process of merging this into Drupal and the script only needs to execute for a specific content type. Not 100% sure what is the best hook to use: hook_node_presave(), hook_load() or hook_node_view()? 

Or should I run this as a template pre-process since this script requires the JS window.location embed?

Part of the code attached:

$web_user_agent = $_SERVER['HTTP_USER_AGENT'];
$current_page = $_SERVER['REQUEST_URI'];

if (!$web_user_agent = '[User Agent Placeholder]') {

    die;

} else {

    echo('&lt;script type="text/javascript"&gt;

    if(self==top) {

            window.location = "'. $redirect_url .'";

    } else {

    }
    &lt;/script&gt;');
    die;
}
It depends on how you want to approach it. Clive's question is still relevant, Javascript redirects are neither great or optimal. However, if it must redirect based on a User Agent, Javascript might be the only option.

I would advise you to do the agent detection on the client side, as the server value may not always be accurate. Your code above won't work.

To load a script based on content type, you can do:

function mytheme_preprocess_node(&amp;$variables) {
  if ($variables['node']-&gt;type == 'mytype') {
    $variables['#attached']['js'][] = array(args);

    // or

    drupal_add_js('path/to/script.js');
  }
}


Your script itself also needs to be in a closure:

/**
 * @file
 */
(function () {

  "use strict";

  // All the JavaScript for this file.

})();


If jQuery or the Drupal scope is needed:

/**
 * @file
 */
(function ($, Drupal) {

  "use strict";

  // All the JavaScript for this file.

})(jQuery, Drupal);


See the docs page for more:


https://www.drupal.org/docs/7/api/javascript-api/managing-javascript-in-drupal-7
https://www.drupal.org/docs/develop/standards/javascript/javascript-coding-standards