Too Cool for Internet Explorer

On-the-fly Picture Swapping, how to get it work in MSIE   June 5th, 2009

To swap or reload a picture on the fly is easy with Javascript. Just give the img tag a unique id and execute the following command in javascript:

document.getElementById("picture").src = "/img/cool-picture.jpg";

Now comes the glitch in (guess where) Internet Explorer: If you want to reload a picture that has changed on the server (let’s say you have overwritten the picture with a php gd-lib script in the background), the Internet Explorer (i call him msie, spoken “emsy”) always keeps the cached picture instead of reloading the picture from the server like opera and firefox do.
You can fix this behaviour with a simple cache-avoiding technique: Add a random number as a parameter, as shown in the following code, that’s it.

document.getElementById("picture").src = "/img/cool-picture.jpg?r=" + Math.floor(Math.random() * 1000);

Tags: , , , , , ,
This entry was posted on Friday, June 5th, 2009 at 15:53 and is filed under Programming. You can follow any responses to this entry through the RSS 2.0 feed.You can leave a response, or trackback from your own site.

No Responses

Leave a Reply