1

If i use something like this to lazy load some of my css files, is it possible to add 2 css files into it or can it only load 1?

<script>
var cb = function() {
            var l = document.createElement('link'); 
            l.rel = 'stylesheet';
            l.href = 'yourCSSfile.css';
            var h = document.getElementsByTagName('head')[0]; 
            h.parentNode.insertBefore(l, h);
         };

var raf = requestAnimationFrame || mozRequestAnimationFrame ||
          webkitRequestAnimationFrame || msRequestAnimationFrame;

if (raf) { 
   raf(cb) 
} else {
    window.addEventListener('load', cb);
}
</script>

1 Answer 1

6
function loadCss(filename) {
    var l = document.createElement('link');
    l.rel = 'stylesheet';
    l.href = filename
    var h = document.getElementsByTagName('head')[0];
    h.parentNode.insertBefore(l, h);
}

function cb() {
    loadCss('yourCSSfile.css');
    loadCss('yourCSSfile2.css');
}

window.addEventListener('load', cb);

http://jsfiddle.net/vs9wLjvp/

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks, how would you do it with the exact code i posted tho?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.