Skip to content

Instantly share code, notes, and snippets.

@xstable
Created February 14, 2018 13:39
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save xstable/a722b83a5db30390e8edd240b40c1f51 to your computer and use it in GitHub Desktop.
Save xstable/a722b83a5db30390e8edd240b40c1f51 to your computer and use it in GitHub Desktop.
cacheBUSTER
// Cache Buster
// https://bgrins.github.io/devtools-snippets/#cachebuster
// Overwrite all link and (optionally) script tags by adding Date.now() at the end of href and src attributes, respectively.
// By default processing scripts is not performed, you should change the variable process_scripts to true to run these.
(function (){
var rep = /.*\?.*/,
links = document.getElementsByTagName('link'),
scripts = document.getElementsByTagName('script'),
process_scripts = false;
for (var i=0;i<links.length;i++){
var link = links[i],
href = link.href;
if(rep.test(href)){
link.href = href+'&'+Date.now();
}
else{
link.href = href+'?'+Date.now();
}
}
if(process_scripts){
for (var i=0;i<scripts.length;i++){
var script = scripts[i],
src = script.src;
if(rep.test(src)){
script.src = src+'&'+Date.now();
}
else{
script.src = src+'?'+Date.now();
}
}
}
})();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment