延迟加载 Javascript 文件与放置在

之前的任何区别
发布于 2024-09-02 23:52:29 字数 241 浏览 2 评论 0原文

环顾四周,找不到讨论这个具体问题的地方。很确定差异可以忽略不计,只是好奇您的想法。

场景:所有不需要在页面呈现之前加载的 Javascript 都放置在结束 标记之前。通过在 DOM 加载/就绪事件触发时执行的头部中的一些 Javascript 代码来延迟加载这些代码有什么好处或坏处?假设这仅涉及下载一个完整的功能齐全的 .js 文件,而不是在使用时根据需要延迟加载多个单独的文件。

希望这是清楚的,谢谢。

原文

Looked around, couldn't find this specific question discussed. Pretty sure the difference is negligible, just curious as to your thoughts.

Scenario: All Javascript that doesn't need to be loaded before page render has been placed just before the closing </body> tag. Are there any benefits or detriments to lazy loading these instead through some Javascript code in the head that executes when the DOM load/ready event is fired? Let's say that this only concerns downloading one entire .js file full of functions and not lazy loading several individual files as needed upon usage.

Hope that's clear, thanks.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

凉月流沐 2024-09-09 23:52:29

在我看来,有很大的不同。

当您在 标记底部内联 JS 时,您将强制页面同步加载这些

如果您希望 DOM 更快地准备好(通常是大多数等待执行事件处理和动画的),则必须尽可能减少所需脚本的大小并并行化它们。

例如,YUI3 有一个小的依赖解析和下载脚本,您必须在页面中按顺序加载该脚本(请参阅 YUI3 的 Seed.js)。之后,您浏览页面并收集依赖项,并对他们的 CDN(或您自己的服务器)进行 1 次异步和管道调用,以获得一大堆 JS。返回 JS 球后,您的脚本将执行您提供的回调。这是一般模式:

<script src="seed.js"></script>
<script>

    YUI().use('module', function(Y) {
        // done when the ball returns and is interpretted
    });

</script>

我不太喜欢将脚本放入 1 个大球中(因为如果 1 个依赖项发生变化,您必须重新下载并解释整个内容!),但我是管道衬里的粉丝(组合脚本)和基于事件的模型。


当您确实允许异步、基于事件的加载时,您会获得更好的性能,但可能不会感觉到性能(尽管这可以被抵消)。

例如,页面的某些部分可能会在一两秒内无法加载,因此看起来会有所不同(如果您使用 JS 来影响页面样式,我建议这样做)或不准备好进行用户交互,直到您(或托管您网站的人员)返回脚本为止。

此外,您必须做一些工作来确保您的

<script>

    $(function () {
        /* do something */
    });

</script>

或者

<script>

    document.observe('dom:loaded', function {
        /* do something */
    });

</script>

解释器会说“Variable $ undefined”之类的内容。即使您同时将两个


所以,选择实际上取决于你。如果你可以正确地分割你的依赖关系——即将你需要的东西放在前面,然后延迟加载其他东西,那么在你到达 DOM 准备就绪之前,这将导致更快的总体时间。

但是,如果您使用像 jQuery 这样的整体库,或者用户希望能够立即看到涉及 JS 动画或样式的内容,那么内联可能更适合您。

There is a big difference, in my opinion.

When you inline the JS at the bottom of the <body> tag, you're forcing the page to load those <script>s synchronously (must happen now) and sequentially (in a row), so you're slowing down the page a bit, as you must wait for those HTTP calls to finish and the JS engine to interpret your scripts. If you're putting lots of JS stacked up together at the bottom of the page, you could be wasting the user's time with network queueing (in older browsers only 2 connections per host at a time), as the scripts may depend on each other, so they must be downloaded in order.

If you want your DOM to be ready faster (usually what most wait on to do any event handling and animation), you must reduce the size of the scripts you need to as little as possible as well as parallelize them.

For instance, YUI3 has a small dependency resolution and downloading script that you must load sequentially in the page (see YUI3's seed.js). After that, you go through the page and gather the dependencies and make 1 asynchronous and pipelined call to their CDN (or your own servers) to get a big ball of JS. After the JS ball is returned, your scripts execute the callbacks you've supplied. Here's the general pattern:

<script src="seed.js"></script>
<script>

    YUI().use('module', function(Y) {
        // done when the ball returns and is interpretted
    });

</script>

I'm not a particularly big fan of putting your scripts into 1 big ball (because if 1 dependency changes, you must download and interpret the whole thing over again!), but I am a fan of pipe-lining (combining scripts) and the event-based model.


When you do allow for asynchronous, event-based loading, you get better performance, but perhaps not perceived performance (though this can be counteracted).

For instance, parts of the page may not load for a second or two, and hence look different (if you're using JS to affect the page style, which I don't advise) or not be ready for user interaction until you (or those hosting your site) return your scripts.

Additionally, you must do some work to ensure your <script>s have the right dependencies to be able to execute properly. For instance, if you don't have jQuery or Prototype, you can't successfully call:

<script>

    $(function () {
        /* do something */
    });

</script>

or

<script>

    document.observe('dom:loaded', function {
        /* do something */
    });

</script>

as the interpretter will say something like "Variable $ undefined". This can happen even if you've added both <script>s to the DOM at the same time, as I'd bet jQuery or Prototype are bigger than you're application's JS (so the request for the data takes longer). Either way, without some type of limiting, you're leaving this up to chance.


So, the choice is really up to you. If you can properly segment your dependencies - i.e. put the stuff you need up front and lazily load the other stuff later, it'll result in faster overall time until you hit the DOM being ready.

However, if you use a monolithic library like jQuery or the user expects to be able to see something involving JS animation or style right away, inlining might be better for you.

冷心人i 2024-09-09 23:52:29

就可用性而言,您绝对不应该对用户期望快速响应的任何内容执行此操作,例如让按钮除了其他功能外还充当加载触发器的双重职责。

OTOH 用用户滚动时不断加载页面来代替分页是一个非常好的方法好主意。我确实发现当加载触发器接近页面末尾时会分散注意力,最好将其放在 1/2 到 3/4 的位置。

In terms of Usability, you definitely shouldn't do this with anything that the user expects a quick response from like having a button do double duty as the load trigger in addition to it's other function.

OTOH replacing pagination with continuously loading the page as the user scrolls is a very good idea. I do find it a distraction when the load trigger is towards the end of the page, better to put it 1/2 to 3/4 the way down.

~没有更多了~

关于作者

无畏

暂无简介

0 文章
0 评论
22 人气
更多

推荐作者

一念一轮回

文章 0 评论 0

脱离于你

文章 0 评论 0

春夜浅

文章 0 评论 0

吃兔兔

文章 0 评论 0

晨曦

文章 0 评论 0

kevin123

文章 0 评论 0

    我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
    原文