简体   繁体   中英

How do I clear the cache from an html page?

Im trying to clear the browser cache when the page is loaded and/or prevent the page from being cached. Here is what I have inside the head and it isn't working:

<meta http-equiv="cache-control" content="no-cache" />
<meta http-equiv="Pragma" content="no-cache" />
<meta http-equiv="Expires" content="-1" />

Your meta code should work with most browsers for web content. However, for resource files (javascript, images, css) your mileage may vary. Most cache busting strategies involve changing the name of your resource files (perhaps dynamically) or using Apache rewrite rules to pretend that the names are changed. This google search should put you on the right track.

cache busting strategy for js

Good Luck.

Your meta tags should be enough for that. However for resources it can vary. Simply attach a variable to the filename, like:

var img = new Image();
img.src = 'image.jpg?'+ Math.random()

or

img.src = 'image.jpg?'+ new Date().getTime()

if you are using javascript to load the resources. Or you can write a rule for mod_rewrite engine of Apache to write a random hash automatically.

However I believe there must be more elegant solution

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