如何防止缓存我的Javascript文件? [英] How to prevent caching of my Javascript file?
问题描述
我有一个简单的html:
I have a simple 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>
在test.js中我更改了Javascript函数,但我的浏览器正在缓存此文件。如何禁用脚本src的缓存?
In test.js I changed a Javascript function, but my browser is caching this file. How to disable cache for script src?
更新:如何使用javascript添加随机脚本?
Update: How to add a random script using javascript ?
推荐答案
将随机查询字符串添加到 src
Add a random query string to the src
您可以通过每次进行更改时递增查询字符串来手动执行此操作:
You could either do this manually by incrementing the querystring each time you make a change:
<script src="test.js?version=1"></script>
或者如果您使用的是服务器端语言,您可以自动生成:
Or if you are using a server side language, you could automatically generate this:
ASP.NET:
<script src="test.js?rndstr=<%= getRandomStr() %>"></script>
有关缓存清除的更多信息,请访问:
More info on cache-busting can be found here:
https://curtistimson.co .uk / post / front-end-dev / what-is-cache-busting /
这篇关于如何防止缓存我的Javascript文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!