`
Tony小熊
  • 浏览: 34486 次
  • 性别: Icon_minigender_1
  • 来自: 北京
社区版块
存档分类
最新评论

Defer loading of JavaScript

阅读更多

Defer loading of JavaScript
Overview

Deferring loading of JavaScript functions that are not called at startup reduces the initial download size, allowing other resources to be downloaded in parallel, and speeding up execution and rendering time.
Details

Like stylesheets, scripts must be downloaded, parsed, and executed before the browser can begin to render a web page. Again, even if a script is contained in an external file that is cached, processing of all elements below the script is blocked until the browser loads the code from disk and executes it. However, for some browsers, the situation is worse than for stylesheets: while JavaScript is being processed, the browser blocks all other resources from being downloaded. For AJAX-type applications that use many bytes of JavaScript code, this can add considerable latency.

For many script-intensive applications, the bulk of the JavaScript code handles user-initiated events, such as mouse-clicking and dragging, form entry and submission, hidden elements expansion, and so on. All of these user-triggered events occur after the page is loaded and the onload event is triggered. Therefore, much of the delay in the "critical path" (the time to load the main page at startup) could be avoided by deferring the loading of the JavaScript until it's actually needed. While this "lazy" method of loading doesn't reduce the total JS payload, it can significantly reduce the number of bytes needed to load the initial state of the page, and allows the remaining bytes to be loaded asynchronously in the background.

To use this technique, you should first identify all of the JavaScript functions that are not actually used by the document before the onload event. For any file containing more than 25 uncalled functions, move all of those functions to a separate, external JS file. This may require some refactoring of your code to work around dependencies between files. (For files containing fewer than 25 uncalled functions, it's not worth the effort of refactoring.)

Tip: Use the Page Speed Profile Deferrable Javascript option to automatically identify candidates for refactoring.

Then, you insert a JavaScript event listener in the head of the containing document that forces the external file to be loaded after the onload event. You can do this by any of the usual scripting means, but we recommend a very simple scripted DOM element (to avoid cross-browser and same-domain policy issues). Here's an example (where "deferredfunctions.js" contains the functions to be lazily loaded):

<script type="text/javascript">

// Add a script element as a child of the body
function downloadJSAtOnload() {
var element = document.createElement("script");
element.src = "deferredfunctions.js";
document.body.appendChild(element);
}

// Check for browser support of event handling capability
if (window.addEventListener)
window.addEventListener("load", downloadJSAtOnload, false);
else if (window.attachEvent)
window.attachEvent("onload", downloadJSAtOnload);
else window.onload = downloadJSAtOnload;

</script>
分享到:
评论

相关推荐

    defer:微型,类型安全,JavaScript原生的Defer实现

    推迟 微小的,类型安全的,JavaScript原生的defer实现。 为什么? 具有可以在函数完成后自动执行的语句会很有帮助。 例如, close文件描述符或清除状态。安装npm install @borderless/defer --save用法 import { ...

    JavaScript无阻塞加载和defer、async详解

    下载是异步的没问题,但是每个javascript执行的时候还是同步的,就是先出现的script标签一定是先执行,即使是并行下载它是最后一个下载完成的,除非标有defer的script标签。任何javascript在执行的时候都会中断当前...

    浏览器环境下JavaScript脚本加载与执行探析之defer与async特性

    defer和async特性相信是很多JavaScript开发者”熟悉而又不熟悉”的两个特性,从字面上来看,二者的功能很好理解,分别是”延迟脚本”和”异步脚本”的作用。然而,以defer为例,一些细节问题可能开发者却并不一定...

    深入理解javascript中defer的作用

    很多人都已经把 Javascript的用的炉火纯青了,但见到defer未必就知道他是做什么用的;很多人也都遇到过这样的问题,需要直接执行别且操作DOM对象的js 总是报找不到对象的错误,原因大家也都知道就是页面还有没有加载...

    【JavaScript源代码】如何在Vue中实现Svelte的Defer Transition.docx

    如何在Vue中实现Svelte的Defer Transition  最近观看了Rich Harris的视频,惊叹于Svelte框架的高效同时,还发现了Vue所不具备的一些关于动画的原生支持—defer transitions. 先看看Svelte所谓的defer transition的...

    async与defer的区别

    形象的描述了async与defer的区别,简单易懂的理解 async是异步执行,异步下载完毕后就会执行,不确保执行顺序,一定在 onload前,但不确定在 DOMContentLoaded事件的前或后 defer是延迟执行,在浏览器看起来的效果像...

    关于Javascript中defer和async的区别总结

    相信看过javascript高级程序设计的人,在javascript高级程序设计里,应该看到了介绍了有关defer和async的区别,可是比较浅显,而且也说得不是很清楚。下面我们来通过这篇文章来详细了解下dfer和async的区别。

    javascript延时加载之defer测试

    偶尔发现 js 中有个延时加载的标签 defer,还在疑惑这么好用的东西为什么没有流行起来。 测试了几个浏览器。在ie7,8,9和360安全济览器下可以。这就是为什么不选择defer的原因了。 代码如下: &lt;!DOCTYPE ...

    前端开源库-defer-promise

    前端开源库-defer-promise延迟承诺,返回延迟承诺的同构函数。使用本机

    GO语言延迟函数defer用法分析

    本文实例讲述了GO语言延迟函数defer用法。分享给大家供大家参考。具体分析如下: defer 在声明时不会立即执行,而是在函数 return 后,再按照 FILO (先进后出)的原则依次执行每一个 defer,一般用于异常处理、释放...

    gulp-defer:将阻止渲染的javascript和CSS移动到延迟加载部分

    将阻止渲染的javascript和css移动到延迟加载部分。 如果您收到PageSpeed Insights结果中的“消除首屏内容中的阻止渲染JavaScript和CSS”警告,则可能会有所帮助 有关如何解决此警告的更多信息,请参见 。 安装 npm ...

    Script中defer的作用

    NULL 博文链接:https://thoreau.iteye.com/blog/747213

    defer的基本使用.go

    defer的基本使用.go

    聊聊golang的defer的使用

    defer函数调用的执行顺序与它们分别所属的defer语句的执行顺序相反 defer后面的表达式可以是func或者是method的调用,如果defer的函数为nil,则会panic 实例 实例1 // f returns 42 func f() (result int) { ...

    material-loading:Vanilla JavaScript Google的类似MaterialDesign的加载模式

    Vanilla JavaScript Google的类似MaterialDesign的加载模式 安装 &lt;!-- include material lite libs --&gt; &lt; link rel =" stylesheet " href =" ...

    golang善用go func和defer

    panic是有秩序的,退出之前会执行完先处理完当前goroutine已经defer【挂上去】的任务,若某个defer在panic之后,则不会被执行。 panic仅保证当前goroutine下的defer都会被调到,但不保证其他协程的defer也会调到 ...

    js的[defer]和[async]属性

    使用async属性加载JavaScript,这样整个脚本就可以异步加载和执行。 [removed]标签的defer属性——告诉浏览器该脚本不会在页面加载完成之前操作DOM,脚本将会和其他资源文件并行下载; [removed]标签的async属性...

Global site tag (gtag.js) - Google Analytics