I have a page which needs to download an image file on onload event but, for all page calls! Now, it downloads the image in first call but in second call it gets from temporary and that is not the thing I want. I need to make some calculations for each load. So I want to delete the file that I download on each load from temporary or something else that forces client to download it from server again.
A javascript solution will be great. Thanks.
No, you can't do anything at all to the cache using Javascript, but you can change the url of the image so that it's not in the cache.
Just add a query string to the url of the image, and use the current time to make it unique each time:
<script type="text/javascript">
document.write('<img src="images/theImage.gif?t=' + new Date().getTime() + '" alt="" />');
</script>
I understand you are looking for a javascript solution, but the good way to solve this is to use HTTP response headers which specify no-cache and expiry values. If you have no control over the web server configuration with .htaccess, you can create a simple PHP script that sets these headers before serving the content. In PHP:
header('Expires: 0');
header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
将URL字符串修改为以下内容:
originalUrl += "?" + new Date().getTime();
我会为正在下载到Cache-Control:no-cache的图像设置HTTP标头,而不是想出一些javascript“hack”。
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.