CSS delivery optimization: How to defer css loading?

前端 未结 6 658
不思量自难忘°
不思量自难忘° 2020-11-28 02:24

I am trying to optimize the CSS delivery following the google documentation for developers https://developers.google.com/speed/docs/insights/OptimizeCSSDeli

6条回答
  •  暖寄归人
    2020-11-28 03:27

    A little modification to the function provided by Fred to make it more efficient and free of jQuery. I am using this function in production for my websites

            // to defer the loading of stylesheets
            // just add it right before the  tag
            // and before any javaScript file inclusion (for performance)  
            function loadStyleSheet(src){
                if (document.createStyleSheet) document.createStyleSheet(src);
                else {
                    var stylesheet = document.createElement('link');
                    stylesheet.href = src;
                    stylesheet.rel = 'stylesheet';
                    stylesheet.type = 'text/css';
                    document.getElementsByTagName('head')[0].appendChild(stylesheet);
                }
            }
    

提交回复
热议问题