タイムスタンプを追加したほうが良いのはなぜですか?
開発プロセス中、バージョンが反復されるときにローカル キャッシュ ファイルがフェッチされるのを避けるために、html の静的リソースに乱数を追加する必要があります (アドレスが同じ場合、ブラウザはこれが同じリクエストであると認識します)。 , css ファイル、js ファイルにタイムスタンプを自動的に付与できます。
- html
<fieldset>
<legend>给url添加时间戳</legend>
<img id="bgimg" alt="">
</fieldset>
- js
<script>
let cacheImg = document.querySelector('[id="bgimg"]')
cacheImg.setAttribute('src', timeStamp('bicycle.gif'))
// 解决项目更换图片浏览器缓存问题
function timeStamp(url) {
let currentTime_stamp = new Date().getTime()
if (url.indexOf('?') > -1) {
url = url.substr(0, url.indexOf("?")) + "?timestamp=" + currentTime_stamp
} else {
url = url + "?timestamp=" + currentTime_stamp
}
return url
}
var Url = window.location.href;
//网页URL添加时间戳
//window.history.pushState 不刷新页面,但是改变页面的url地址
window.history.pushState({}, 0, timeStamp(Url));
console.log(window.location.href);
</script>