File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff line change @@ -805,6 +805,33 @@ HTTP协议支持“分块编码”。它允许将页面分成一块一块发送
805805
806806frist_script是页面中一定存在的一个script标签,script是你创建的新的script元素。
807807
808+ ### 延迟加载
809+
810+ 所谓的延迟加载是指在页面的load事件之后再加载外部文件。通常,将一个大的合并后的文件分成两部分是有好处的:
811+
812+ - 一部分是页面初始化和绑定UI元素的事件处理函数必须的
813+ - 第二部分是只在用户交互或者其它条件下才会用到的
814+
815+ 目标就是逐步加载页面,让用户尽快可以进行一些操作。剩余的部分可以在用户可以看到页面的时候再在后台加载。
816+
817+ 加载第二部分JavaScript的方法也是使用动态script元素,将它加在head或者body中:
818+
819+ .. The full body of the page ...
820+
821+ <!-- end of chunk #2 -->
822+ <script src="all_20100426.js"></script>
823+ <script>
824+ window.onload = function () {
825+ var script = document.createElement("script");
826+ script.src = "all_lazy_20100426.js";
827+ document.documentElement.firstChild.appendChild(script);
828+ };
829+ </script>
830+ </body>
831+ </html>
832+ <!-- end of chunk #3 -->
833+
834+ 对很多应用来说,延迟加载的部分大部分情况下会比核心部分要大,因为我们关注的“行为”(比如拖放、XHR、动画)只在用户初始化之后才会发生。
808835
809836
810837
You can’t perform that action at this time.
0 commit comments