search
HomeWeb Front-endHTML Tutorial如何使用预加载技术来提升网页加载速度?_html/css_WEB-ITnose

 预加载器(Pre-loader)可以说是提高浏览器性能最重要的举措。Mozilla 官方发布数据,通过预加载器技术网页的加载性能提升了19%,Chrome测试了 Alexa 排名前2000名网站,性能有20%的提升。

  它并不是一门新技术,有人认为只有 Chrome 才具备这个功能。也有人认为它是有史以来提升浏览器性能最有效的方法。如果你第一次接触预加载器,也许心中已经有了无数个问号。什么是预加载器?它是如何提升浏览器性能的?

 首先需要了解浏览器是如何加载网页的

  一个网页的加载依赖于脚本文件、CSS样式文件。让我们看看浏览器加载网页的过程。

  • 首先,浏览器下载 HTML 并开始解析。如果浏览器发现外部 CSS 资源链接则发送下载请求。
  • 浏览器可以在下载 CSS 资源的同时,并行解析HTML文件,但是,一旦发现有脚本文件的引用,则必须等待脚本文件完成下载并且执行后才能继续解析。
  • 脚本文件完成下载并且执行后,浏览器可以继续解析HTML工作,如果遇到非阻塞资源 i.e. 图片浏览器会发送下载请求并且继续解析。
  •   即使浏览器可以并行执行多个请求,但是无法与针对脚本文件的操作并行执行。

      可以通过IE7打开链接中的网页进行测试。我们可以看到,网页head标签内包含2个样式文件和2个脚本文件。在body中,包含3个图片、1个脚本文件。

      通过瀑布流我们可以查看资源加载的过程:

      脚本文件的下载和执行,会阻断其他资源文件的下载,无疑将大大降低浏览器性能。

     预加载器如何提高网络利用率

      2008 年,IE、WebKit和Mozilla都实现了预加载器功能,来提升网络的利用率,改善脚本文件对其他资源文件的阻塞现状。

      当浏览器被脚本文件阻塞时,另一个轻量级的解析器会继续浏览剩余的标记,寻找需要下载的资源i.e. 样式文件, 脚本文件,图片 等。

      一旦发现,预加载器既可以在后台开始接收这些资源,等待主解析器完成当前的脚本操作,其他资源已经完成下载,这样就减轻了脚本阻塞带来的性能损耗。

      下面这个瀑布流是使用IE8打开链接中网页的结果,性能有显著的提升:IE8=7S > IE7=14S。

      预加载功能仍然是各大浏览器厂商乐此不疲的实验领域。很多浏览器尝试设置资源下载的优先级。例如,Safari降低了不作用于当前视图区域样式文件的优先级。Chrome 则设置脚本文件的优先级高于图片,即使脚本文件位于HTML底部。

     预加载器的陷阱

      预加载器只能检索 HTML 标签中的URL,无法检测到使用脚本代码添加的URL,直至脚本代码执行时才可以获取这类资源。

      我曾经遇到过一个通过 JavaScript 判断当前Window宽度,进而决策加载CSS样式文件的例子。预加载器无法识别此类资源。

    1

    2

    3

    4

    5

    6

    7

    8

    9

    10

    11

    12

    13

    14

    15

    16

      <script> </script>

      var  file = window.innerWidth

      document.write( '' );

     

     

     

      如何使用预加载技术来提升网页加载速度?_html/css_WEB-ITnose

      如何使用预加载技术来提升网页加载速度?_html/css_WEB-ITnose

      如何使用预加载技术来提升网页加载速度?_html/css_WEB-ITnose

      如何使用预加载技术来提升网页加载速度?_html/css_WEB-ITnose

      如何使用预加载技术来提升网页加载速度?_html/css_WEB-ITnose

      如何使用预加载技术来提升网页加载速度?_html/css_WEB-ITnose

     

      上面这段代码可以轻松的骗过IE9的预加载机制,在下面的瀑布流中我们可以看到,加载图片占用了所有的连接,直至第一个图片加载完成后,CSS文件才开始下载。

     影响预加载器的加载顺序的因素

      当前,有几种方式来控制预加载器的加载顺序(使用javacript隐藏资源文件既是其中一种),同时,W3C Resource Priorities 中也提供两个特性来影响预加载器。

      lazyload : 直至没有被标记为lazyload 资源下载完毕后才下载被标记资源。

      postpone: 资源在对最终用户可见之后才开始下载。i.e. 标签的display属性被设置为 none。

     预加载VS预读取

      预读取(Pre-fetching)可以通知浏览器哪些资源可能会在未来的某一时机,在当前页面或者其他页面中使用。

      下面是预读取的一个简单的应用,通知浏览器为将要访问的其他站点加载资源:

    1

      Chrome允许我们预先通知浏览器加载未来会用到的资源,被声明的资源将以较高的优先级被下载。

    1

    (Chromium 源码中提到,被标记为subresource的资源下载的优先级低于样式文件和脚本文件,但不低于图片加载优先级)

      还有标记可以通知浏览器哪些文件是较低级别的预读取文件。

      预读取未来将被使用的独立资源文件。

    1

      通过预读取方式,在后台渲染整个页面。

    1

     总结

      预加载不是一门新技术,它对提高浏览器性能具有纪念意义,我们不需要做任何操作既可以使用预加载。

    Statement
    The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
    Difficulty in updating caching of official account web pages: How to avoid the old cache affecting the user experience after version update?Difficulty in updating caching of official account web pages: How to avoid the old cache affecting the user experience after version update?Mar 04, 2025 pm 12:32 PM

    The official account web page update cache, this thing is simple and simple, and it is complicated enough to drink a pot of it. You worked hard to update the official account article, but the user still opened the old version. Who can bear the taste? In this article, let’s take a look at the twists and turns behind this and how to solve this problem gracefully. After reading it, you can easily deal with various caching problems, allowing your users to always experience the freshest content. Let’s talk about the basics first. To put it bluntly, in order to improve access speed, the browser or server stores some static resources (such as pictures, CSS, JS) or page content. Next time you access it, you can directly retrieve it from the cache without having to download it again, and it is naturally fast. But this thing is also a double-edged sword. The new version is online,

    How do I use HTML5 form validation attributes to validate user input?How do I use HTML5 form validation attributes to validate user input?Mar 17, 2025 pm 12:27 PM

    The article discusses using HTML5 form validation attributes like required, pattern, min, max, and length limits to validate user input directly in the browser.

    How to efficiently add stroke effects to PNG images on web pages?How to efficiently add stroke effects to PNG images on web pages?Mar 04, 2025 pm 02:39 PM

    This article demonstrates efficient PNG border addition to webpages using CSS. It argues that CSS offers superior performance compared to JavaScript or libraries, detailing how to adjust border width, style, and color for subtle or prominent effect

    What are the best practices for cross-browser compatibility in HTML5?What are the best practices for cross-browser compatibility in HTML5?Mar 17, 2025 pm 12:20 PM

    Article discusses best practices for ensuring HTML5 cross-browser compatibility, focusing on feature detection, progressive enhancement, and testing methods.

    What is the purpose of the <datalist> element?What is the purpose of the <datalist> element?Mar 21, 2025 pm 12:33 PM

    The article discusses the HTML <datalist> element, which enhances forms by providing autocomplete suggestions, improving user experience and reducing errors.Character count: 159

    What is the purpose of the <progress> element?What is the purpose of the <progress> element?Mar 21, 2025 pm 12:34 PM

    The article discusses the HTML <progress> element, its purpose, styling, and differences from the <meter> element. The main focus is on using <progress> for task completion and <meter> for stati

    How do I use the HTML5 <time> element to represent dates and times semantically?How do I use the HTML5 <time> element to represent dates and times semantically?Mar 12, 2025 pm 04:05 PM

    This article explains the HTML5 <time> element for semantic date/time representation. It emphasizes the importance of the datetime attribute for machine readability (ISO 8601 format) alongside human-readable text, boosting accessibilit

    What is the purpose of the <meter> element?What is the purpose of the <meter> element?Mar 21, 2025 pm 12:35 PM

    The article discusses the HTML <meter> element, used for displaying scalar or fractional values within a range, and its common applications in web development. It differentiates <meter> from <progress> and ex

    See all articles

    Hot AI Tools

    Undresser.AI Undress

    Undresser.AI Undress

    AI-powered app for creating realistic nude photos

    AI Clothes Remover

    AI Clothes Remover

    Online AI tool for removing clothes from photos.

    Undress AI Tool

    Undress AI Tool

    Undress images for free

    Clothoff.io

    Clothoff.io

    AI clothes remover

    AI Hentai Generator

    AI Hentai Generator

    Generate AI Hentai for free.

    Hot Article

    R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
    2 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
    Repo: How To Revive Teammates
    4 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
    Hello Kitty Island Adventure: How To Get Giant Seeds
    3 weeks agoBy尊渡假赌尊渡假赌尊渡假赌

    Hot Tools

    WebStorm Mac version

    WebStorm Mac version

    Useful JavaScript development tools

    SublimeText3 Linux new version

    SublimeText3 Linux new version

    SublimeText3 Linux latest version

    ZendStudio 13.5.1 Mac

    ZendStudio 13.5.1 Mac

    Powerful PHP integrated development environment

    SublimeText3 Mac version

    SublimeText3 Mac version

    God-level code editing software (SublimeText3)

    SublimeText3 English version

    SublimeText3 English version

    Recommended: Win version, supports code prompts!