当今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); });
因为 XHR 的下载过程是异步的,所以这个过程中浏览器图标不会显示『忙提示』。 JS的执行时间很短暂,可以认为页面始终不会停止响应。 XHR 有跨域问题,因此该方法只适用于资源位于同一域名的情况(或者开启CORS响应头字段)。
我们知道 eval() 方法是不安全的,除了使用 eval() 方法,我们还可以创建一个 <script> 标签,并把XHR获取的脚本注入进去。 效果是一样的。 </script>
Iframe
缺点:不能跨域,开销较大,原页面的JS不可直接用于Iframe
通过在DOM中插入一个Iframe,让Iframe里的脚本操作当前页面的DOM。 通过 parent.document 可以获得父页面的DOM。
// JS in Iframevar doc = parent.document;doc.body.appendChild(xxx);
浏览器的同源策略也不允许Iframe的JS访问跨域的父页面,因此该方法也不能跨域。 同时浏览器需要为Iframe创建完整的一套DOM并进行渲染,开销较大。
DOM Element
优点:可跨域
可能读者也想到了,直接在页面里插入一个 <script> 不就可以了吗! 确实可以,还能实现跨域呢。看代码: </script>
var script = $('<script>', {src: '/path/to/sth.js'});$('head').append(script);
这是比较流行的解决方案。
Defer/Async
优点:可以灵活控制优先页面渲染还是优先执行脚本。
缺点:Opera浏览器尚不支持。
既然在脚本执行时我们插入新的 <script> 标签可以延迟脚本加载; 那么我们能否在编写HTML时直接说明这个 <script> 要延迟加载呢? 是可以的! </script>
HTML的 <script> 标签有两个属性用来声明延迟加载: defer 和 async 。 其中 async 是HTML5标准提出的,不幸的是这俩属性在Opera中仍未得到支持。 </script>
<script src="one.js" async></script> <!--异步执行--><script src="one.js" defer></script> <!--延迟执行-->
这两者有什么区别呢?请看:
图片来自 peter.sh 。
正常执行
在下载和执行脚本时HTML停止解析
defer执行
在下载脚本时HTML仍然在解析,HTML解析完成后再执行脚本。延迟执行不会阻塞渲染,额外的好处是脚本执行时页面已经渲染结束。
async执行
在下载脚本时HTML仍然在解析,下载完成后暂停HTML解析立即执行脚本。

HTMLtagsdefinethestructureofawebpage,whileattributesaddfunctionalityanddetails.1)Tagslike,,andoutlinethecontent'splacement.2)Attributessuchassrc,class,andstyleenhancetagsbyspecifyingimagesources,styling,andmore,improvingfunctionalityandappearance.

The future of HTML will develop in a more semantic, functional and modular direction. 1) Semanticization will make the tag describe the content more clearly, improving SEO and barrier-free access. 2) Functionalization will introduce new elements and attributes to meet user needs. 3) Modularity will support component development and improve code reusability.

HTMLattributesarecrucialinwebdevelopmentforcontrollingbehavior,appearance,andfunctionality.Theyenhanceinteractivity,accessibility,andSEO.Forexample,thesrcattributeintagsimpactsSEO,whileonclickintagsaddsinteractivity.Touseattributeseffectively:1)Usese

The alt attribute is an important part of the tag in HTML and is used to provide alternative text for images. 1. When the image cannot be loaded, the text in the alt attribute will be displayed to improve the user experience. 2. Screen readers use the alt attribute to help visually impaired users understand the content of the picture. 3. Search engines index text in the alt attribute to improve the SEO ranking of web pages.

The roles of HTML, CSS and JavaScript in web development are: 1. HTML is used to build web page structure; 2. CSS is used to beautify the appearance of web pages; 3. JavaScript is used to achieve dynamic interaction. Through tags, styles and scripts, these three together build the core functions of modern web pages.

Setting the lang attributes of a tag is a key step in optimizing web accessibility and SEO. 1) Set the lang attribute in the tag, such as. 2) In multilingual content, set lang attributes for different language parts, such as. 3) Use language codes that comply with ISO639-1 standards, such as "en", "fr", "zh", etc. Correctly setting the lang attribute can improve the accessibility of web pages and search engine rankings.

HTMLattributesareessentialforenhancingwebelements'functionalityandappearance.Theyaddinformationtodefinebehavior,appearance,andinteraction,makingwebsitesinteractive,responsive,andvisuallyappealing.Attributeslikesrc,href,class,type,anddisabledtransform

TocreatealistinHTML,useforunorderedlistsandfororderedlists:1)Forunorderedlists,wrapitemsinanduseforeachitem,renderingasabulletedlist.2)Fororderedlists,useandfornumberedlists,customizablewiththetypeattributefordifferentnumberingstyles.


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

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

Hot Article

Hot Tools

SAP NetWeaver Server Adapter for Eclipse
Integrate Eclipse with SAP NetWeaver application server.

MinGW - Minimalist GNU for Windows
This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.

Zend Studio 13.0.1
Powerful PHP integrated development environment

ZendStudio 13.5.1 Mac
Powerful PHP integrated development environment

mPDF
mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),
