javascript - CSS 交付优化 : How to defer css loading?

标签 javascript html css google-pagespeed deferred-loading

我正在尝试按照开发人员的谷歌文档优化 CSS 交付 https://developers.google.com/speed/docs/insights/OptimizeCSSDelivery#example

正如您在内联小型 CSS 文件的示例中看到的那样,关键 CSS 内联在头部,原始 small.css 在页面加载后加载

<html>
  <head>
    <style>
      .blue{color:blue;}
    </style>
    </head>
  <body>
    <div class="blue">
      Hello, world!
    </div>
  </body>
</html>
<noscript><link rel="stylesheet" href="small.css"></noscript>

关于这个例子我的问题:

如何在页面加载后加载一个大的css文件?

最佳答案

如果您不介意使用 jQuery,这里有一个简单的代码片段可以帮助您。 (否则评论,我会写一个纯js的例子

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

只需在您的 $(document).ready()window.onload 函数中调用它即可。

对于 #2,您为什么不尝试一下呢?在您的浏览器中禁用 Javascript 并查看!

顺便说一句,一个简单的 google 搜索可以让您走多远,这真是令人惊讶;对于查询 "post load css",这是第四次命中... http://www.vidalquevedo.com/how-to-load-css-stylesheets-dynamically-with-jquery

关于javascript - CSS 交付优化 : How to defer css loading?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19374843/

相关文章:

javascript - 使用 jQuery 获取查询集合中除第一个元素之外的所有元素的方法是什么。

javascript - iOs 和 Android : create a bookmark -shortcut- in homescreen by javascript

javascript - Angular + Websockets + 数据渲染

html - :before selector not taking full height 中的 css 边框样式

c# - 无法将 Kendo 网格中的大数据导出到 Excel

jquery - 日期选择器字段为只读

javascript - 如何将下拉列表的值传递给 instafeed tagName 属性

HTML/CSS : Content growing larger than 960 grid

html - 当我尝试并排显示两个 DIVS 时,右边的比左边的低

javascript - 可以居中吗?