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'] = 
     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');
  • Even this method gives me the same result. The JS is getting attached to all the pages. I want it to get attached only where the block is set to be visible.
    – Vivek
    Feb 24 '12 at 7:47
  • Just change it to $content = theme('header_theme'); if (!empty($content)) { drupal_add_js(drupal_get_path('module', 'mymodule').'/sample.js'); $block['content'] = $content; }
    – Clive
    Feb 24 '12 at 8:53
  • Are you enabled js aggregation in drupal? Feb 24 '12 at 9:33
  • No, I have js aggregation disabled.
    – Vivek
    Feb 24 '12 at 9:56
  • Did you canfigure this block in admin page like "show this block to the follwing page" - block configuration Feb 24 '12 at 10:25

If this apply css to individual blocks / nodes become a frequent requirment take a look at code per node, we have found it very useful for our knowledge system when we sometimes rely on custom code for advanced jquery data tables or charts etc.

  • I tried code per node module. Even this module attaches the js on all the pages. But anyways, thanks for your answer.
    – Vivek
    Feb 24 '12 at 8:30
  • Hmm not sure what you mean by attaches the js to all pages? It doesn't seem to in our implementation when using nodes. Can you elaborate? What caching do you use?
    – WestieUK
    Feb 24 '12 at 8:39
  • I have normal caching mode and page compression enabled . I have a block which is visible only in the front page, and I have this script $(document).ready( function() { alert("Test"); } )}; written in the text area provided by code per node module. Now I expect this alert message to be displayed only in the front page, but instead it gets displayed in all the pages i.e the script is attached to all the pages. So this is what happens to me. Is there anything I am doing wrong here? I will be very glad if you could help me.
    – Vivek
    Feb 24 '12 at 9:36
  • Ill test on my environment now and report back shortly...
    – WestieUK
    Feb 24 '12 at 10:43
  • Tested and the js doesn't persist across pages, I done exactly same as you and the js alert only shows on home page. It must be something on your configuration...?
    – WestieUK
    Feb 24 '12 at 14:03

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.