Created
February 23, 2010 20:51
-
-
Save NV/312690 to your computer and use it in GitHub Desktop.
CSS hot reloading
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // 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