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.


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.

  • 1
    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.

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.