Skip to content

Instantly share code, notes, and snippets.

@NV
Created February 23, 2010 20:51
Show Gist options
  • Select an option

  • Save NV/312690 to your computer and use it in GitHub Desktop.

Select an option

Save NV/312690 to your computer and use it in GitHub Desktop.
CSS hot reloading
// Inspired by http://github.com/adamlogic/auto_update_stylesheets
/**
* Usage: document.styleSheets[0].reload()
*/
CSSStyleSheet.prototype.reload = function reload(){
var href = this.href;
if (href) {
var i = href.indexOf('?'),
last_reload = 'last_reload=' + (new Date).getTime();
if (i < 0) {
href += '?' + last_reload;
} else if (href.indexOf('last_reload=', i) < 0) {
href += '&' + last_reload;
} else {
href = href.replace(/last_reload=\d+/, last_reload);
}
return this.ownerNode.href = href;
}
};
/**
* Usage: document.styleSheets.reload()
*/
StyleSheetList.prototype.reload = function reload(){
for (var i=0; i<this.length; i++) {
this[i].reload()
}
};
/**
* Usage: document.styleSheets.start_autoreload()
*/
StyleSheetList.prototype.start_autoreload = function start_autoreload(miliseconds){
if (!start_autoreload.running) {
var styles = this;
start_autoreload.running = setInterval(function reloading(){
styles.reload();
}, miliseconds || 300);
}
return start_autoreload.running;
};
/**
* Usage: document.styleSheets.stop_autoreload()
*/
StyleSheetList.prototype.stop_autoreload = function stop_autoreload(){
clearInterval(this.start_autoreload.running);
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment