简体   繁体   English

我可以在javascript中删除临时Internet文件中的文件吗?

[英]Can I delete file from temporary internet files in javascript?

I have a page which needs to download an image file on onload event but, for all page calls! 我有一个页面需要在onload事件上下载图像文件,但是,对于所有页面调用! 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. 一个JavaScript解决方案将是伟大的。 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. 不,你不能使用Javascript对缓存做任何事情,但你可以更改图像的URL,使其不在缓存中。

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. 我知道你正在寻找一个javascript解决方案,但解决这个问题的好方法是使用HTTP响应头,它指定no-cache和expiry值。 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. 如果您无法使用.htaccess控制Web服务器配置,则可以创建一个简单的PHP脚本,在提供内容之前设置这些标头。 In PHP: 在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”。

You definitely can't delete a temporary file with javascript. 你绝对不能用javascript删除临时文件。 To avoid caching use the no-cache header when sending the image. 为避免缓存,请在发送映像时使用no-cache标头。 This way the image will be downloaded every time is requested. 这样,每次请求时都会下载图像。

List of headers here . 这里的标题列表。

You must use: 你必须使用:

Cache-Control: no-cache 缓存控制:无缓存

This is Microsoft specific. 是微软特有的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM