web的缓存html问题
第一种方案
为了强制加载,在文件名后加上类似于hash串的时间戳或者随机数。
找了很久大佬们的解决方案,感觉下面这个最好用。
<script>document.write("<script type='text/javascript' src='//site.com/js.js?v=" + Date.now() + "'><\/script>");</script>
第二种方案
打包时加md5,这样就不会取到老的文件。
入口文件index.html不使用缓存。或者每次访问时都在后面加个随机的参数。
?t=" + Date.now()
第三种方案
index.html不使用缓存。或者每次访问时都在后面加个随机的参数。
更改<script src="src/settings.js" charset="utf-8"></script>
里settings.js的地址。"main.js"不需要怎么改,不会动的。
10秒内更新index.html
Request = GetRequest();
var t = parseInt(Request["t"]);
if(t){
if((t+1000*10) < Date.now()){
window.location.href='./index.html?t=' + Date.now();
}
}else{
window.location.href='./index.html?t=' + Date.now();
}
function GetRequest() {
var url = location.search;
var theRequest = new Object();
if (url.indexOf("?") != -1) {
var str = url.substr(1);
strs = str.split("&");
for (var i = 0; i < strs.length; i++) {
theRequest[strs[i].split("=")[0]] = unescape(strs[i].split("=")[1]);
}
}
return theRequest;
}