简体   繁体   中英

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. We have a large amount of html and xml files generated by our site that a user can see. 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. I have tried simulating a form submit with "?submit=....." at the end but it didn't work.

tl;dr What javascript function can open a page and tell the browser to get the newest version, ignoring cache?

In JavaScript, I think the only way to prevent caching is to modify the 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. If you need a fresh version, the easiest way is to add a timestamp to the URL's query string.

<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.

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.

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