简体   繁体   English

我可以用Javascript按钮强制浏览器忽略缓存吗?

[英]Can I make a Javascript button force the browser to ignore cache?

I know there is a lot about this, but I can't find a solution that fits my situation. 我知道有很多关于此的内容,但是我找不到适合我情况的解决方案。 I am following behind someone else's asp.net code. 我跟踪别人的asp.net代码。 We have a large amount of html and xml files generated by our site that a user can see. 我们有大量的由我们的网站生成的html和xml文件,用户可以看到。 In one place, the link dynamically generated to load one of these pages is actually in a miniature form, making the browser think data is being submitted and looking for something 'new.' 在一个地方,动态生成以加载这些页面之一的链接实际上是一种微型形式,使浏览器认为数据正在提交并且正在寻找“新的”东西。 But the other is a button with the link generated in the vb code behind using a javascript function to open the page in a new window. 但是另一个是按钮,该按钮具有使用JavaScript函数在新窗口中打开页面的,在vb代码中生成的链接。 I have tried simulating a form submit with "?submit=....." at the end but it didn't work. 我尝试过模拟结尾处带有“?submit = .....”的表单提交,但是它不起作用。

tl;dr What javascript function can open a page and tell the browser to get the newest version, ignoring cache? tl; dr哪些JavaScript函数可以打开页面并告诉浏览器获取最新版本,而忽略缓存?

In JavaScript, I think the only way to prevent caching is to modify the url. 在JavaScript中,我认为防止缓存的唯一方法是修改url。 One trick is to use the current date as timestamp: 一种技巧是使用当前日期作为时间戳:

url = url + "?_ts="+new Date().getTime();

(of course if your url already includes a querystring then replace the ? with an &) (当然,如果您的网址已经包含一个查询字符串,则用&替换?)

There is no JavaScript to do what you need. 没有JavaScript可以满足您的需求。 If you need a fresh version, the easiest way is to add a timestamp to the URL's query string. 如果需要新版本,最简单的方法是在URL的查询字符串中添加时间戳。

<script type="text/javascript" src="/js/file?cacheBuster=<?= DateTime.Now ?>" > </script>

For better control, you can use a build version as your cacheBuster param so you don't have to request new files every time. 为了更好地控制,您可以将构建版本用作cacheBuster参数,这样就不必每次都请求新文件。

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

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