初始化JS分量HTML或接通QUOT的端部;的onload&QUOT ;? [英] Initializing JS components at the end of HTML or on "onload"?

查看:149
本文介绍了初始化JS分量HTML或接通QUOT的端部;的onload&QUOT ;?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有一段时间,我一直在运行的JavaScript组件初始化被等待的onload事件触发和执行各种各样的的main()。这似乎更清洁,可以确保您的DOM的ID状态是为了。但之后将通过其步伐的一段时间,我发现该组件的初始化是负载(图片,CSS,内部框架,闪光灯等)时哽咽着用任何一种资源挂。

For a while I had been running JavaScript component initialization by waiting for the "onload" event to fire and executing a main() of sorts. It seemed cleaner and you could be sure the the ID state of your DOM was in order. But after some time of putting that through its paces I found that the component's initialization was choked off by any sort of resource hanging during the load (images, css, iframes, flash, etc.).

现在我提出用内联&LT初始化调用到HTML文档本身的结束;脚本/> 执行,并发现它推前其他外部的初始化资源。

Now I have moved the initialization invocation to the end of the HTML document itself using inlined <script /> execution and found that it pushes the initialization before other external resources.

现在我不知道是否有一些缺陷附带这样做,而不是等待的onload。

Now I wonder if there are some pitfalls that come with doing that instead of waiting for the "onload".

哪种方法您使用?

修改:谢谢。似乎每个图书馆都有专门的功能 DOMContentLoaded / 的readyState 实现差异。我使用的原型,以便是我需要的。

EDIT: Thanks. It seems each library has a specialized function for DOMContentLoaded/readyState implementation differences. I use prototype so this is what I needed.

推荐答案

对于我来说,我们使用jQuery,以及它的文档就绪状态确保DOM被加载,而不是等待资源像你说的。当然,这没有一个JavaScript框架,你可以,但它需要一个功能,您可以创建例如:文件准备现在,在大多数情况下把脚本在页面的结尾肯定保证页面的其余部分是存在的,但要确保DOM就绪从来都不是一件坏事。

For me, we use jquery, and its document ready state ensures that the DOM is loaded, but is not waiting for resources like you say. You can of course to this without a javascript framework, it does require a function you can create for example: document ready Now, for the most part putting script at the end of the page sure ensure the rest of the page is there, but making sure the DOM is ready is never a bad thing.

这篇关于初始化JS分量HTML或接通QUOT的端部;的onload&QUOT ;?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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