I added this code to template.php (in Zen subtheme):

function mytheme_preprocess_page(&$vars) {
  if ($vars['is_front']) {
    drupal_add_js (libraries_get_path('jquery.cycle') . '/jquery.cycle.all.js', 'file');

Just after adding it, each block (e.g. drupal search block, my custom blocks etc) and also this preprocess function are executed twice. I checked it using a debugger. The output on pages is OK, but executing blocks twice has performance implications.

By the way, the same happens if the JavaScript file is added in mytheme_preprocess_html() instead of mytheme_preprocess_page.

Is hook_preprocess_page() the right place to add JavaScript code? (I guess not.) Is there another way of adding JavaScript to a specific page other than the above? (I know I can add in mytheme.info, but it will add to each page, whereas it's needed only on front page.)

  • drupal_add_js() should not cause what you are noticing, as its code essentially sets a static variable. Probably there is some module that is causing what you see. – kiamlaluno Feb 29 '12 at 19:44
  • You might be right. I am investigating the "less" module now, it looks like switching it on and off has the result of processing blocks once or twice. But I had the same result after adding javascript, so not sure what is really the culprit here - still checking. – camcam Feb 29 '12 at 21:49

I finally discovered two circumstances when blocks are executed twice:

First is adding a missing javascript file, either in theme.info file or in theme_preprocess_page() function using drupal_add_js.

Second is by adding a broken url(..) image reference in a css file which is actually used on the page.

Here are the steps to recreate the problem with css, for javascript it's analogously:

  • install Drupal (it has Bartik theme as default) and log in, to see the search box
  • in a debugger, set a breakpoint inside function search_block_view()
  • refresh the page and check that the breakpoint is reached once
  • go to /themes/bartik/css/styles.css line 1467 and change the line containing:

background: url(../images/search-button.png)

into something that does not exist:

background: url(../images/search-button1.png)

  • refresh the page with shift and check that the breakpoint is reached twice

Maybe such behavior is obvious for someone more advanced and has rational explanation, but for me it is not obvious and took me a while to discover. Anyway the conclusion is just to make sure that all loaded css and javascript resources are available and nothing is missing, otherwise blocks will be silently executed twice and depending what they contain, the page may load longer.

  • This behaviour is because drupal handles 404 errors rather than leaving it to Apache and in doing so an entire page build process happens. – Rimu Atkinson May 13 '16 at 1:52

As far as I know drupal_add_js should be used in a module. What you can do is to implement hook_init and check $_GET['q'] for the path of your frontpage.

function hook_init() {
  if ($_GET['q']=='path/to/your/frontpage') {
    drupal_add_js( ... );
  • If it is the same as drupal_add_css(), then drupal_add_js() can be called from a theme in hook_preprocess_html(). – kiamlaluno Feb 29 '12 at 19:39

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.