Reputation: 97
I have a page with an image on it, however this image changes quite frequently, but it has the same URL. To bypass the browser from caching an older version of the image, I want to add a random number variable after the URL. What is the best way to do this with Javascript?
So when the page loads, it does not load:
http://test.com/image.png
instead it loads:
http://test.com/image.png?43673890
So each time the page is reloads, it has a new variable so the latest version of the image is visible.
Upvotes: 0
Views: 2378
Reputation: 15566
Give your img tag an id, then you can
document.getElementById("imageid").src=document.getElementById("imageid").src + "?" + Math.random()* 100000000000000000000;
if the initial url has ? then do
document.getElementById("imageid").src=document.getElementById("imageid").src.split('?')[0] + "?" + Math.random()* 100000000000000000000;
Upvotes: 1