Where can I add this jquery code to apply to a view block? I have some jquery that I need to apply to a view that's being displayed as a block. The only template we're using for this particular view is views-view-grouping--my-view.tpl.php. I've tried adding the jquery to the beginning of this file, but as expected (because it runs recursively for each group) it's being added to the rendered source before every rendered group, instead of just once.

Where is the best place for me to add jquery code so that it's only added to the rendered source once? It's just two lines of code aside from the document.ready declaration, so I'd rather not have to put it in a separate file and call it conditionally from various page templates.
One quick way could be to add it inline in a preprocess function.

    /**
     * Implements template_preprocess_views_view().
     */
    function THEMENAME_preprocess_views_view(&amp;$vars) {
      $view = $vars['view'];
      // Add Javascript when the view with the name news is shown and the display id is your-display-id.
      if ($view-&gt;name == 'news' &amp;&amp; $view-&gt;current_display == 'your-display-id') {
         drupal_add_js('jQuery(document).ready(function () { alert("Hello!"); });', 'inline');
      }
    }


If you prefer you could also add it as a file that you store in your theme. This would include a file called myfile.js under the folder for your theme /js

    /**
     * Implements template_preprocess_views_view().
     */
    function THEMENAME_preprocess_views_view(&amp;$vars) {
      $view = $vars['view'];
      // Add Javascript when the view with the name news is shown and the display id is your-display-id.
      if ($view-&gt;name == 'news' &amp;&amp; $view-&gt;current_display == 'your-display-id') {
         global $theme_path;
         drupal_add_js($theme_path . '/js/myfile.js');
      }
    }