添加到 DOM 的内容是否计入页面总加载时间?

发布于 2024-10-25 05:28:36 字数 251 浏览 9 评论 0原文

我的意思是,如果我附加一些这样的内容:

<body>
 //contents
 <script>body.appendChild('<img src="new.png">');
 // other contents
</body>

浏览器将仅考虑原始 html 来触发 window.onload 还是也会考虑新图像的加载? (new.png)?

I mean if I append some contents like this:

<body>
 //contents
 <script>body.appendChild('<img src="new.png">');
 // other contents
</body>

the browser will fire window.onload considering only the original html or it will take in consideration the load of the new image too? (new.png) ?

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

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

发布评论

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

评论(1

攒眉千度 2024-11-01 05:28:36

除了代码/标记不正确之外,它还会考虑新图像。将其附加到 DOM 将下载 src 属性指向的任何内容。

但是,如果将此代码放置在 window.onload = function() { ... } 内,则不会考虑它,因为在加载窗口之前不会进行下载。

这是实际有效的代码...

var img = new Image;

img.src = 'http://www.gravatar.com/avatar/3535689c965d66db3d2a936ced96192a?s=32&d=identicon&r=PG';
img.alt = 'Example';

document.body.appendChild(img);

jsFiddle

Besides that code/markup being incorrect, it will consider the new image. To append it to the DOM will be to download whatever the src attribute points to.

However, if this code was placed inside of a window.onload = function() { ... }, then it wouldn't be considered because its download would not occur until your window was loaded.

Here is the code that would actually work...

var img = new Image;

img.src = 'http://www.gravatar.com/avatar/3535689c965d66db3d2a936ced96192a?s=32&d=identicon&r=PG';
img.alt = 'Example';

document.body.appendChild(img);

jsFiddle.

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