Skip to content

Instantly share code, notes, and snippets.

@meagar
Last active October 13, 2015 12:38
Show Gist options
  • Save meagar/4197363 to your computer and use it in GitHub Desktop.
Save meagar/4197363 to your computer and use it in GitHub Desktop.
Refresh only CSS via ctrl-R
/**
* Include via a <script> tag and reload CSS files via ctrl-R without a full-page refresh.
* Maintains your state, especially useful for JavaScript-based single-page apps
*
* Requires jQuery 1.6 or greater
*/
(function () {
"use strict";
$(window).bind('keypress', function (event) {
// Ctrl-R
if (event.ctrlKey && event.charCode === 18) {
event.preventDefault();
if (console && console.log) {
console.log('CSS REFRESH');
}
$('link[rel=stylesheet]').each(function (i, e) {
e = $(e);
var href = e.attr('href');
var match = href.match(/[&?]cssrefreshmne=(\d+)/);
if (match) {
// already has our query string param
href = href.replace(/cssrefreshmne=\d+/, 'cssrefreshmne=' + new Date().getTime());
} else {
// need to add our query string param
href += (href.match(/\?/) ? '&' : '?');
href += 'cssrefreshmne=' + new Date().getTime();
}
e.attr('href', href);
})
}
});
})();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment