浏览器添加随机数去除缓存

  <script>
    /*缓存加随机版本号,需要给哪个文件加直接把路径传进来调方法*/
writeVersion("http://apps.bdimg.com/libs/accounting.js/0.3.2/accounting.min.js,http://apps.bdimg.com/libs/accounting.js/0.3.2/accounting.min.js,http://apps.bdimg.com/libs/todc-bootstrap/3.1.1-3.2.1/todc-bootstrap.min.css");
function writeVersion(path){
    var pathArray = path.split(",");
    var scriptArray ='';
    if(pathArray.length>0){
        for(var i = 0; i< pathArray.length; i++){
            if(pathArray[i].indexOf(".js")>-1){
                scriptArray+="<script src='"+pathArray[i]+"?v="+Math.floor(Math.random()*100000000)+"'><"+'/'+"script>";
            }else if(pathArray[i].indexOf(".css")>-1){
                scriptArray+="<link rel='stylesheet' href='"+pathArray[i]+"?v="+Math.floor(Math.random()*100000000)+"'>";
            }
        }
    }
    document.write(scriptArray);
}
  </script>

如果对于比较固定的版本,不需要随时变动的话,可以配置一个固定的版本号,当有变动的时候,改变版本号。

posted @ 2018-06-11 09:41  马小乐  阅读(282)  评论(0编辑  收藏  举报