Skip to content

Instantly share code, notes, and snippets.

@marioizquierdo
Created September 13, 2012 00:40
Show Gist options
  • Save marioizquierdo/3711034 to your computer and use it in GitHub Desktop.
Save marioizquierdo/3711034 to your computer and use it in GitHub Desktop.
Press Ctrl+E to reload page CSS
$(function() {
// Reload All Stylesheets
var reloadCSS = function() {
var $stylesheets;
$stylesheets = $('link[rel=stylesheet]');
$stylesheets.each(function(i, el) {
var $stylesheet, href, newHref, noCacheToken;
$stylesheet = $(el);
href = $stylesheet.attr('href');
noCacheToken = (new Date).getTime();
if (href.indexOf("nocache") >= 0) {
newHref = href.replace(/nocache=\d+/, "nocache=" + noCacheToken);
} else {
newHref = "" + href + (href.indexOf('?') >= 0 ? '&' : '?') + "nocache=" + noCacheToken;
}
$stylesheet.attr('href', newHref);
});
};
// Reload CSS when pressing ctrl+e (cmd+e on mac)
$(window).keydown(function(e) {
if (e.which === 69 && (e.metaKey || e.ctrlKey)) {
reloadCSS();
}
});
});
@marioizquierdo
Copy link
Author

It uses jQuery.
The idea is listen for Crtl+E (which is very close to Crtl+R) to reload ONLY the stylesheets in the page.
This is non intrusive, it doesn't require any configuration in the server at all, it plays nicely with Rails Asset Pipeline, and it definitely speeds up CSS development.

It won't update the CSS in real time, for that you should look at Guard LiveReload or similar

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