如何防止缓存我的 Javascript 文件? [英] How to prevent caching of my Javascript file?
本文介绍了如何防止缓存我的 Javascript 文件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个简单的 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?
推荐答案
向 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屋!
查看全文