Project:Nitobe
Version:6.x-4.1
Component:Code
Category:bug report
Priority:normal
Assigned:Unassigned
Status:closed (works as designed)
Issue tags:header, image, Twice

Issue Summary

I follwed the instructions in #348568: phptemplate_preprocess_page is called twice for sub-themes, but the header image is loading twice.

I found out:

The 1st time the image is loading by nitobe_utils.inc's :

function _nitobe_fixed_header_css($filename) {
  global $base_url;

  $url    = $base_url . '/' . $filename;
  $output = '<style type="text/css">#masthead{background-image:url(%s);}</style>';

  return sprintf($output, $url);
}

The 2nd time by another function of nitobe_utils.inc's :

function _nitobe_random_header_js() {
  global $base_url;

  $files = _nitobe_get_header_list();
  $names = array();

  foreach ($files as $file => $data) {
    $names[] = $base_url . '/' . $file;
  }

  $name_js = drupal_to_js($names);

  $js = <<<EOJS
<script type="text/javascript">
  $(document).ready(function() {
    var names = {$name_js};
    $('#masthead').css('background-image', 'url(' + names[Math.floor(Math.random() * names.length)] + ')');
  });
</script><noscript></noscript>
EOJS;

  return $js;
}

Can I prevent this behavior?

Comments

#1

My workaround is to disable the return of javascript in the 2nd code.

...
EOJS;

//  return $js;
}

#2

Thanks for that, worked for me. Be nice to see it fixed in the theme.

#3

Status:active» closed (works as designed)

The random header JavaScript is not output unless the random image option has been chosen in the theme's configuration.

Since a random header relies on JavaScript, a backup has to be provided for clients that do not have JavaScript enabled. Thus the first load of the image is done by CSS to guarantee that clients that do not have JavaScript enabled still get an image. Otherwise, the header image would be just an empty area on clients with JavaScript disabled.