异步资源加载完成后会触发哪些Javascript事件? [英] What Javascript event fires when async resources are finished loading?

查看:71
本文介绍了异步资源加载完成后会触发哪些Javascript事件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果我像这样异步加载脚本

If I load a script asynchronously like this

<script type="text/javascript">
    (function () {
        var js = document.createElement('script');
        js.type = 'text/javascript';
        js.async = true;
        js.src = '<%=BundleTable.ResolveUrl("~/js") %>';
        var s = document.getElementsByTagName('script')[0];
        s.parentNode.insertBefore(js, s);
    })();
</script>

异步内容完成加载并且dom完成加载后会触发什么事件?

what event fires once the async stuff is finished loading and the dom is finished loading?

推荐答案

每个事件都有自己的通知机制,因此没有通用的答案。例如,您可以阅读有关如何检测动态加载脚本何时完成加载此处此处。这些天,你可以通过使用像YUI或jQuery这样的好框架来节省大量的时间,因为他们大多数都有对这些类型的事件的内置跨浏览器支持。

Every event has it's own notification mechanism so there is no generic answer. For example, you can read about how to detect when a dynamically loaded script is done loading here and here. These days, you can save yourself a ton of time by using a good framework like YUI or jQuery as they most have built-in cross-browser support for these types of events.

对于DOM完成加载,没有单一的跨浏览器机制可以在加载完成后尽可能早地获取。如果你看一下$(document).ready()的jQuery实现,你会看到几种技术(监听DOMContentLoaded事件,setTimeout()最早会触发,监听窗口load(),文档onreadystatechange事件等...)以便跨浏览器支持此功能。

For the DOM finished loading, there is no single cross-browser mechanism that gets you the earliest possible time when it's finished loading. If you look at a jQuery implementation of $(document).ready(), you will see several techniques employed (listening for "DOMContentLoaded" event, the earliest time that a setTimeout() will fire, listening for window load(), document onreadystatechange event, etc...) in order to support this functionality cross browser.

window.onload 事件将保证安全的DOM访问,但它也等待所有图像完成加载,这比必要的晚。这些其他技术用于获得更早的安全访问。

The window.onload event will guarentee safe DOM access, but it also waits for all images to finish loading which is much later than neccessary. These other techniques are used to get earlier safe access.

这篇关于异步资源加载完成后会触发哪些Javascript事件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆