Table of Contents
什么是脚本阻塞
XHR
Iframe
DOM Element
Defer/Async
正常执行
defer执行
async执行
Home Web Front-end HTML Tutorial 最佳实践:非阻塞脚本载入_html/css_WEB-ITnose

最佳实践:非阻塞脚本载入_html/css_WEB-ITnose

Jun 24, 2016 am 11:24 AM

当今Web页面和脚本已经越来越复杂,页面性能有时会变得很差。 这常常被误解为网络或浏览器的问题,其实前端技术对页面性能有着非常显著的影响。 前端对页面性能的影响也是多方面的:

  • 脚本/样式文件的划分会影响浏览器缓存效率。
  • 频繁的DOM操作会占用大量CPU资源。
  • 资源链接的先后顺序也会影响页面渲染速度。
  • 同步的网络请求会使页面停止响应。

本文将探讨不同的脚本载入方式对页面性能的影响, 包括『浏览器忙提示』、『页面停止渲染』、『光标停止响应』等。 最终给出无阻塞脚本载入的最佳实践。

什么是脚本阻塞

本文的主题是无阻塞脚本载入技术,那么什么是脚本阻塞呢?

在 Web性能优化:prefetch, prerender 一文中提到, 浏览器在获取资源时会经过DNS解析、建立连接、下载文件、渲染页面等过程。 浏览器只会同时下载3-5个脚本,然后顺序执行它们。 并且在下载和执行过程中,页面会停止渲染和响应(也就是我们说的页面卡死)。

性能最差的情形是某个脚本下载超时,后面的脚本和样式都会被阻塞很长时间, 浏览器的图标会一直处于忙的状态。 如果这个脚本出现在HTML HEAD标签中,阻塞过程中整个页面都将是空白,用户体验极差。

为了防止脚本阻塞,我们来探讨非阻塞的脚本载入方式。 采用这种脚本在如方式时,浏览器(以Chrome为例)不会显示忙指示器图标, 页面也不会停止响应。

XHR

缺点:不能跨域,容易被劫持

我们知道 XHR 可以用来执行异步的网络请求,XHR Eval方法的原理便是通过XHR下载整个脚本,通过 eval() 函数来执行这个脚本。

$.get('/path/to/sth.js')    .done(function(src){        eval(src);    });
Copy after login

因为 XHR 的下载过程是异步的,所以这个过程中浏览器图标不会显示『忙提示』。 JS的执行时间很短暂,可以认为页面始终不会停止响应。 XHR 有跨域问题,因此该方法只适用于资源位于同一域名的情况(或者开启CORS响应头字段)。

我们知道 eval() 方法是不安全的,除了使用 eval() 方法,我们还可以创建一个

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

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Is HTML easy to learn for beginners? Is HTML easy to learn for beginners? Apr 07, 2025 am 12:11 AM

HTML is suitable for beginners because it is simple and easy to learn and can quickly see results. 1) The learning curve of HTML is smooth and easy to get started. 2) Just master the basic tags to start creating web pages. 3) High flexibility and can be used in combination with CSS and JavaScript. 4) Rich learning resources and modern tools support the learning process.

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 &lt;progress&gt; element, its purpose, styling, and differences from the &lt;meter&gt; element. The main focus is on using &lt;progress&gt; for task completion and &lt;meter&gt; for stati

The Roles of HTML, CSS, and JavaScript: Core Responsibilities The Roles of HTML, CSS, and JavaScript: Core Responsibilities Apr 08, 2025 pm 07:05 PM

HTML defines the web structure, CSS is responsible for style and layout, and JavaScript gives dynamic interaction. The three perform their duties in web development and jointly build a colorful website.

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 &lt;datalist&gt; element, which enhances forms by providing autocomplete suggestions, improving user experience and reducing errors.Character count: 159

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 &lt;meter&gt; element, used for displaying scalar or fractional values within a range, and its common applications in web development. It differentiates &lt;meter&gt; from &lt;progress&gt; and ex

Understanding HTML, CSS, and JavaScript: A Beginner's Guide Understanding HTML, CSS, and JavaScript: A Beginner's Guide Apr 12, 2025 am 12:02 AM

WebdevelopmentreliesonHTML,CSS,andJavaScript:1)HTMLstructurescontent,2)CSSstylesit,and3)JavaScriptaddsinteractivity,formingthebasisofmodernwebexperiences.

What is an example of a starting tag in HTML? What is an example of a starting tag in HTML? Apr 06, 2025 am 12:04 AM

AnexampleofastartingtaginHTMLis,whichbeginsaparagraph.StartingtagsareessentialinHTMLastheyinitiateelements,definetheirtypes,andarecrucialforstructuringwebpagesandconstructingtheDOM.

Gitee Pages static website deployment failed: How to troubleshoot and resolve single file 404 errors? Gitee Pages static website deployment failed: How to troubleshoot and resolve single file 404 errors? Apr 04, 2025 pm 11:54 PM

GiteePages static website deployment failed: 404 error troubleshooting and resolution when using Gitee...

See all articles