项目中解决缓存问题

//给css和script添加随机字符串 jq
var scriptLen=$('script').length;
for(var i=0;i<scriptLen;i++){
  $('script')[i].setAttribute('src',$('script')[i].getAttribute('src')+'?'+new Date().getTime())
}
var linktLen=$('link').length;
for(var i=0;i<linktLen;i++){
  $('link')[i].setAttribute('href',$('link')[i].getAttribute('href')+'?'+new Date().getTime())
}  
--------------------------------------

//给css和script添加随机字符串  js dom

var scriptLen=document.getElementsByTagName("script").length;
for(var i=0;i<scriptLen;i++){
  document.getElementsByTagName("script")[i].setAttribute('src',document.getElementsByTagName("script")[i].getAttribute('src')+'?'+new Date().getTime())
}
var linktLen=document.getElementsByTagName("link").length;
for(var i=0;i<linktLen;i++){
 

使用方法: 将以上代码新建一个js: timestamp.js  直接放在每个页面的最下面引用即可。

2017-11-07

原文地址:https://www.cnblogs.com/gongxiaoyan/p/7797698.html