Bdf*_*dfy 111 javascript caching
我有一个简单的HTML:
<html>
<body>
<head>
<meta charset="utf-8">
<meta http-equiv='cache-control' content='no-cache'>
<meta http-equiv='expires' content='0'>
<meta http-equiv='pragma' content='no-cache'>
<script src="test.js"></script>
</body>
</html>
Run Code Online (Sandbox Code Playgroud)
在test.js中我更改了Javascript函数,但我的浏览器正在缓存此文件.如何禁用脚本src的缓存?
更新:如何使用javascript添加随机脚本?
Cur*_*urt 164
添加随机查询字符串 src
您可以通过每次进行更改时递增查询字符串来手动执行此操作:
<script src="test.js?version=1"></script>
Run Code Online (Sandbox Code Playgroud)
或者,如果您使用的是服务器端语言,则可以自动生成:
ASP.NET:
<script src="test.js?rndstr=<%= getRandomStr() %>"></script>
Run Code Online (Sandbox Code Playgroud)
有关缓存清除的更多信息,请访问:
https://curtistimson.co.uk/post/front-end-dev/what-is-cache-busting/
Ale*_*tau 32
<script src="test.js?random=<?php echo uniqid(); ?>"></script>
Run Code Online (Sandbox Code Playgroud)
编辑:或者你可以使用文件修改时间,以便它缓存在客户端上.
<script src="test.js?random=<?php echo filemtime('test.js'); ?>"></script>
Run Code Online (Sandbox Code Playgroud)
dav*_*ode 11
您可以将queryString附加到src并仅在发布更新版本时更改它:
<script src="test.js?v=1"></script>
Run Code Online (Sandbox Code Playgroud)
通过这种方式,浏览器将使用缓存版本,直到指定新版本(v = 2,v = 3 ...)
您可以将随机(或日期时间字符串)作为查询字符串添加到指向您的脚本的URL.像这样:
<script type="text/javascript" src="test.js?q=123"></script>
Run Code Online (Sandbox Code Playgroud)
每次刷新页面时,都需要确保"q"的值已更改.