How to restrict JS applied to a block only to block visible pages? I am using Drupal 6. I have added the javascript to my block using hook_theme as shown below.

function theme_custom($text) {

        drupal_add_js(drupal_get_path('module', 'mymodule').'/sample.js');
        $output =  t($text);
        return $output;
}


I am using $(document).ready(function() { ... }); in my sample.js. I want this js to be loaded only on the pages where the block is set to be visible, but drupal loads this js on all the pages. This makes my script to run on all the pages, increasing the load to the server on every page load. So, how to restrict this js to be loaded only where the block is visible? Any help is much appreciated.
Using hook_block you can show the js wherever the block renders.

function hook_block($op = 'list', $delta = 0) {
   $block['content'] = 
   switch($op){
     case 'view':
      switch ($delta) {
        case 'header_block'://block name
        //here you add that js
          drupal_add_js(drupal_get_path('module', 'mymodule').'/sample.js');
          $block['content'] = theme('header_theme');
          break;       
      }         
      break;
   }
}