在 HTML 文件中引用 JavaScript 文件时,可以通过在文件路径后面加上时间戳的方式来防止缓存。例如:

<script src='myScript.js?t=123456789'></script>

其中,'t=123456789' 就是时间戳,可以使用 JavaScript 中的 Date 对象获取当前时间戳,如下所示:

<script>
  const timestamp = Date.now();
  const script = document.createElement('script');
  script.src = `myScript.js?t=${timestamp}`;
  document.head.appendChild(script);
</script>

这样,每次页面加载时,都会使用不同的时间戳来引用 JavaScript 文件,从而避免缓存导致的问题。

如何通过时间戳避免 JavaScript 文件缓存

原文地址: https://www.cveoy.top/t/topic/oRYC 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录