Created
May 5, 2015 19:49
-
-
Save kidGodzilla/7048a8c66d3dc85e9d2e to your computer and use it in GitHub Desktop.
JavaScript Cache Buster Example
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* 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