Skip to content

Instantly share code, notes, and snippets.

@kidGodzilla
Created May 5, 2015 19:49
Show Gist options
  • Save kidGodzilla/7048a8c66d3dc85e9d2e to your computer and use it in GitHub Desktop.
Save kidGodzilla/7048a8c66d3dc85e9d2e to your computer and use it in GitHub Desktop.
JavaScript Cache Buster Example
/**
* Generates a random hex string
*/
function s4() {
return Math.floor((1 + Math.random()) * 0x10000)
.toString(16)
.substring(1);
}
/**
* Append `?cb=` to each download of a `.pdf` file
*/
$('a').each(function () {
var href = $(this).attr('href');
if (href.indexOf(".pdf") > 0 && href.indexOf("?") === -1)
$(this).attr('href', href + "?cb=" + s4() + "" + s4());
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment