Bookmarklet: Reload CSS files without reloading the page

Working on styling the contents of popup windows can be frustrating and laborious. But not if you can just reload the CSS…

This bookmarklet will reload all the associated stylesheets in the current document. It looks for all <link/> elements with rel="stylesheet", removes from the DOM and re-adds them appending a ?_=1234567890 (or &_ depending on the URL, where the numbers represent the JS timestamp) into the <head/>.

Hopefully this will help someone suffering similar pain to myself!

Reload CSS (Add the link to the left to your bookmarks to use!)

Please note: I haven’t tested this in IE, but surely it’ll work… Right?

Full commented code:

(function(document, getElementsByTagName, toString, match, href, length, i) {
    var head = document[getElementsByTagName]('head')[0],
        links = document[getElementsByTagName]('link'),
        store = [];

    // iterate over all the existing s
    for (; i < links[length]; i++) {
        // if it's a stylesheet
        if (links[i][href][toString]()[match](/\.css/)) {
            // clone the element, false is needed as Firefox requires the deep flag

            // then remove it from the DOM

            // decrement i, because links is a NodeList (not an array) and when the element is
            // removed from the DOM, it is also removed from the NodeList

    // iterate over the stored items
    while (store[length]) {
        // strip out any previous cachebusting querystring
        store[0][href] = store[0][href][toString]().replace(/([&\?])_=\d+/, '');
        store[0][href] = store[0][href] + (store[0][href][toString]()[match](/\?/) ? '&' : '?') + '_=' +[toString]();

        // put the element in the 
})(document, 'getElementsByTagName', 'toString', 'match', 'href', 'length', 0);

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.