Skip to content

Instantly share code, notes, and snippets.

@mikelikespie
Created October 27, 2010 18:31
Show Gist options
  • Save mikelikespie/649650 to your computer and use it in GitHub Desktop.
Save mikelikespie/649650 to your computer and use it in GitHub Desktop.
This refreshes all the css on a page by changing the query string
/*
Add a bookmark to this
javascript:(function(){var c=document.getElementsByTagName("link");for(var d=0;d<c.length;d++){var a=c[d];if(a.rel=='stylesheet'||a.type=="text/css"){var e="css_buster_"+Math.floor(Math.random()*1000000000);var g=a.href.split("?",2);var f;if(g.length>1){var b=g[1].indexOf("&")==-1;if(b){f=e}else{f=g[1]+"&"+e}}else{f=e}a.href=g[0]+"?"+f}}})();
*/
(function() {
var links = document.getElementsByTagName('link');
for (var i = 0; i < links.length; i++) {
var l = links[i];
if (l.rel == 'stylesheet' || l.type == 'text/css') {
var rand_n = 'css_buster_' + Math.floor(Math.random() * 1000000000);
var splitted = l.href.split('?', 2);
var new_query_string;
// Does it already have a query string?
if (splitted.length > 1) {
// does it have params or just a cache buster
var has_amp = splitted[1].indexOf('&') == -1; // is it a query string?
// if it's just a cache buster, swap it out
if (has_amp) {
new_query_string = rand_n;
} else {
new_query_string = splitted[1] + '&' + rand_n;
}
} else {
new_query_string = rand_n;
}
l.href = splitted[0] + '?' + new_query_string;
}
}
})()
@mikelikespie
Copy link
Author

@jney Oh dang, you are indeed correct. I assumed the behavior was similar to python's .split. I guess JavaScript, Java, and Ruby is the number of elements max, whereas python is the number of splits max. Fixing code.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment