首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >谷歌PageSpeed洞察说我的网站很重。外部通道是一个解决方案吗?

谷歌PageSpeed洞察说我的网站很重。外部通道是一个解决方案吗?
EN

Stack Overflow用户
提问于 2016-03-17 13:55:25
回答 1查看 125关注 0票数 0

我正在为我办公室的文件夹开发这个一页的网站,为了保持它的整洁,我使用引导模式来隐藏一些内容(相当多的图像和描述)。根据谷歌PageSpeed Insights的说法,这使得整个页面变得沉重。为了寻找解决方案,我在考虑将所有的模态加载为外部html文件,使用以下代码:

代码语言:javascript
复制
www.jsfiddle.net/qp7NP/

我不知道这对我的情况来说是不是最聪明的解决方案。我也在关注搜索引擎优化。我读到过AJAX以某种方式用于动态加载内容,但我对AJAX一点也不熟悉。如果你们中的任何人知道我的情况的任何解决方案或可以在页面速度问题上给我建议,我将非常感激!

谢谢大家!

EN

回答 1

Stack Overflow用户

发布于 2016-03-18 10:13:13

AJAX真的不像听起来那么可怕,对于这样的东西来说,它是一个很好的选择,因为它允许你用几行JavaScript加载大量的内容,这意味着无论额外的内容如何,初始加载时间都更短。

下面的示例演示了如何将“折叠下方”(在起始滚动位置下方)作为HTML加载,并在需要的地方插入。

代码语言:javascript
复制
<!DOCTYPE html>
<html>
<head>
    <title>example</title>
</head>
<body>
    <h1>Page Title.</h1>
    <p>This is the content which is on the screen at load</p>

    <!-- this is the div which will hold all content outside of view when the page loads -->
    <div id="below_fold"></div>

    <script>
    // get the content on once the page has loaded
    window.onload = function() {
      // set up http request object
      var xhttp = new XMLHttpRequest();
      // tell http object what to do when response is recieved
      xhttp.onreadystatechange = function() {
        // if its all good then go ahead
        if (xhttp.readyState == 4 && xhttp.status == 200) {
          // add content to div
          document.getElementById("below_fold").innerHTML = xhttp.responseText;
        }
        // else we don't do anything, you could display an error or try again
      };
      // set file path for contents
      xhttp.open("GET", "below_fold_content_html.txt", true);
      // send request, xhttp.onreadystatechange will be called when completed
      xhttp.send();
    }
    </script>
</body>
</html>

(上面的示例是对http://www.w3schools.com/ajax/的添加)

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/36052677

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档