CSS delivery optimization: How to defer css loading?

If you don’t mind using jQuery, here is a simple code snippet to help you out. (Otherwise comment and I’ll write a pure-js example

function loadStyleSheet(src) {
    if (document.createStyleSheet){
        document.createStyleSheet(src);
    }
    else {
        $("head").append($("<link rel="stylesheet" href=""+src+" />"));
    }
};

Just call this in your $(document).ready() or window.onload function and you”re good to go.

For #2, why don’t you try it out? Disable Javascript in your browser and see!

By the way, it’s amazing how far a simple google search can get you; for the query "post load css", this was the fourth hit…
http://www.vidalquevedo.com/how-to-load-css-stylesheets-dynamically-with-jquery

Leave a Comment