Invoke javascript in hook
D7 you can try function drupal_add_js

function hook_preprocess_node(&amp;$vars){
  drupal_add_js(drupal_get_path('theme', 'your_theme') . '/js/my_script.js');
}


D8 You need define one or more (asset) libraries, add a *.libraries.yml file to the root of your theme folder

library_name:
  version: 1.x
  js:
    js/my_script.js: {}


Attaching a library in a preprocess function

function hook_preprocess_node(&amp;$variables){
  $variables['#attached']['library'][] =  'your_theme/library_name';
}