Add random variable after image url with javascript

夙愿已清 提交于 2019-12-13 04:34:26

问题


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.


回答1:


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;


来源:https://stackoverflow.com/questions/17394440/add-random-variable-after-image-url-with-javascript

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!