Skip to content

Instantly share code, notes, and snippets.

@keithnorm
Created July 10, 2010 21:22
Show Gist options
  • Save keithnorm/471041 to your computer and use it in GitHub Desktop.
Save keithnorm/471041 to your computer and use it in GitHub Desktop.
// Instead of refreshing the page each time and letting Rails regenerate your css files from any changed Sass files, use the Sass watch script:
// sass --watch public/stylesheets/sass:public/stylesheets
//
// Then use javascript to force the new css to load
function refreshCSS() {
var i,a,s;
a=document.getElementsByTagName('link');
for(i=0;i<a.length;i++){
s=a[i];
if(s.rel.toLowerCase().indexOf('stylesheet')>=0&&s.href) {
var h=s.href.replace(/(&|%5C?)forceReload=\d+/,'');
s.href=h+(h.indexOf('?')>=0?'&':'?')+'forceReload='+(new Date().valueOf())
}
}
}
// You can bind this function to window focus (useful for jumping between editor and browser and seeing changes instantly)
$(document).bind('focus', function(e){
refreshCSS();
});
// or bind it to a key command
$(document).bind('keydown', function(e){
// mapped to ctrl + R, but feel free to change it if you want
if(e.keyCode == 82 && e.ctrlKey)
refreshCSS();
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment