Invoke javascript in hook I have create a hook to alter page funcionality. It's posible invoke javascript function in hook.
          I test this function but i had this error -->  Call to undefined function alert()

      function catalogo_preprocess_node(&amp;$vars)
      {
        alert("hello");
      }
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';
}