WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I'm working on a site that is using this PHP script to load a random header image every time it reloads.

I would like to use Javascript instead to allow the browser to cache images and improve load times and was wondering if there's any benefit to using the PHP script over some JS script for this scenario.

share|improve this question
up vote 0 down vote accepted

Using PHP sends less code to the end user. (a single img tag instead of all the javascript logic). If you enable browser caching settings correctly (try W3 Total Cache), once the end user has "seen" all the images, those will all be in their cache, so on the "long tail" JavaScript vs. PHP solution is equivalent.

share|improve this answer
you mention cache- if you set up static caching and cache the php script that sends a random image, you'll always get the same image, so you'll have to exclude that script, but that means having to fire up php on every request when otherwise it may not be necessary. in that case I'd say sending some extra js bytes is better. – Milo Aug 3 '11 at 19:25

An idiot, but working solution would be to add a hidden div with the lowest min. size and pre-load the images there. That'd cache it without any high-tech super-scripts.

share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

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