在html中,给引用的js文件动态添加版本号,可以去掉js缓存,保证使用的js始终是最新的js 。
<!DOCTYPE html>
<html>
<head>
<title> 动态的为js添加版本号,去除缓存 </title>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<meta http-equiv="Expires" content="0">
<meta http-equiv="Pragma" content="no-cache">
<meta http-equiv="Cache-control" content="no-cache">
<meta http-equiv="Cache" content="no-cache">
<link href="css/public.css" rel="stylesheet" type="text/css" />
<link href="css/cplb.css" rel="stylesheet" type="text/css" />
<script src="js/jquery-2.1.1.min.js" type="text/javascript"></script>
<!-- 通过下面的方式,动态的为js添加版本号,去除缓存 -->
<script>
document.write('<script src="http://localhost:8000/aaaa.js?v='+Math.random()+'"><\/script>');
document.write('<script src="bbbb.js?v='+Math.random()+'"><\/script>');
document.write('<script src="http://localhost:8000/cccc.js?v='+Math.random()+'"><\/script>');
</script>
<script src="js/xxxx.js" type="text/javascript"></script>
<script type="text/javascript">
// JS 代码
</script>
</head>
<body>
<h1>给引用的js文件动态添加版本号 </h1>
</body>
</html>