add js by #attached - hook_page_alter in theme's template.php I am trying to add js by #attached, as I understand it's better from a caching perspective then drupal_add_js(). I try to use hook_page_alter() in template.php.
I have the following code:     

function mytheme_page_alter(&amp;$page) {
 $theme_path = drupal_get_path('theme', 'mytheme' );

  if (!isset($page['content']['#attached']))
    {
      $page['content']['#attached'] = array
      (
        'js' =&gt; array(),
      );
    }

  $page['content']['#attached']['js'][] = array($theme_path . '/test.js');
}


(this is adapted from http://www.jaypan.com/tutorial/including-javascript-or-css-files-every-page-load-module-drupal-7, I first tried to attach [#attached] directly to $page by means of  $page['#attached'] = array... , but as I faced the problem described below I tried to test some code suggested by other people). 

The problem is that the elements of the js are not properly set. After the 'js' =&gt; array(), The Aptana Debugger shows what I'd expect : 


After $page['content']['#attached']['js'][] = array($theme_path . '/test.js'); , however, the array values are not set:

Still the value of array($theme_path . '/test.js')is what it 'should' be:
   

It's as if the ascription did not go through.
What did I do wrong?
I think your problem is you're trying to use a hook from a theme template.php? (Assuming because you have mytheme_page_alter as your function name for hook_page_alter). If you're not getting the JS included, it's specifically because you can't use hooks in template.php files. You need to create a new module.