View Full Version : Resolved Need Javascript to force an image refresh for Firefox

06-13-2008, 05:43 AM
I am using Matt Mullenweg's php random image rotator. There is an issue with the refresh due to cacheing on Firefox. A javascript solution was posted on his site, but I don't really know javascript and can't get it to work.

This is the solution that was outlined:


Dave Achtemichuk | October 20th, 2007 @ 10:23 pm
I work often with sites/pages that use server-side caching, so PhilMills’ solution above doesn’t work (as the random number that’s generated would get cached also!). In the past I’ve instead implemented the same approach, but in Javascript. I’m not sure if I can paste code here, so I’ll just explain how:

1) Output your IMG tag as normal, or using the solution immediately above (this will ensure it still works if JavaScript is disabled)
2) Give the IMG tag an HTML ID (such as ‘randimage’)
3) Immediately after the IMG tag include some inline javascript ( using “script” tags … also within a CDATA comment if doctype is xhtml): “document.getElementById(’randimage’).src = document.getElementById(’randimage’).src + ‘?rand=’ + (Math.random() * 10000000);”
Hope this helps someone.

Can someone please write this out for me? I'm unsure of what this is telling me to do. :confused:

06-13-2008, 08:11 AM
Well, the use of a random number isn't the best idea, those can repeat. A number generated by the Date object would be a better choice, it's constantly updated and never repeats. This is the sort of code described in your post, only using the Date object instead of the Math object's random function to generate a unique query:

<img src="whatever.jpg" id="some_id">
<script type="text/javascript">
document.getElementById('some_id').src = document.getElementById('some_id').src + '?unique=' + new Date().valueOf();