Tuesday, February 7, 2012

Image (<IMG>) SRC Dynamic Update Using Script Not Working in IE

If you have HTML code running this dynamically changing image:



<script>

document.getElementById('myID').src="myscript";

</script>



HTML:

<img src="myscript" id=myID>



This won't work in IE. You need this:



<script>

var randomNo=Math.floor(Math.random()*10000);

document.getElementById('myID').src="myscript?"+randomNo;

</script>



As IE will refer to cache data if it sees the data is the same as the next click.
You can also change to other methods of generating random numbers. As long as the query string is different every time, IE will not refer to cache data and fetch from the server instead.
Read More »