强制app刷新图像(来自URL) [英] force app to refresh image (from URL)
问题描述
我的应用程序(android)中有一个来自URL的图像:
I have an image in my app (android) taken from an URL:
<img src="URL"/>
如果我更改服务器上的图像(保持相同的URL),则应用程序不刷新始终保持旧图像。
If i change the image on the server (keeping the same URL), the app does not refresh and keeps always the old image.
如何强制应用重新加载/刷新正确的图像(始终在同一网址上)?
How can i force the app to reload/refresh the right image (always on the same URL)?
(应用程序是用jquery编写的)
(the app is written with jquery)
推荐答案
尝试将查询字符串附加到图像网址每次显示图像时更改的随机数。
Try appending a querystring to the image url with a random number that you change each time you display the image.
例如,可以使用javascript完成此操作。
This may for example be done with javascript.
<img id="myImage" src="URL"/>
<script>
var newRandomNumber = (new Date()).valueOf();
var img = document.getElementById('myImage');
img.src += "?random=" + newRandomNumber;
</script>
这个脚本将获取id为myImage的html元素,并附加一个querystring参数(希望如此)强制重新加载图像。
This script will get the html element with the id myImage and append a querystring parameter which will (hopefully) force a reload of the image.
(NB值(new Date())。代码中的valueOf()不是随机的,而是一个代表当前时间的数字)
(NB the value (new Date()).valueOf() in the code is not random but a number that represents the current time)
希望这会有所帮助!
这篇关于强制app刷新图像(来自URL)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!