Last active
October 13, 2015 12:38
-
-
Save meagar/4197363 to your computer and use it in GitHub Desktop.
Refresh only CSS via ctrl-R
This file contains 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
/** | |
* 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