最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Initializing JS components at the end of HTML or on "onload"? - Stack Overflow

programmeradmin4浏览0评论

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

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.

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

Which method are you using?

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

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

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.

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

Which method are you using?

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

Share Improve this question edited Aug 30, 2020 at 20:46 HoldOffHunger 21k11 gold badges120 silver badges146 bronze badges asked Apr 3, 2009 at 16:36 wreelwreel 956 bronze badges
Add a ment  | 

5 Answers 5

Reset to default 4

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.

Jquery has $(document).ready()

The ideal point at which to run most scripts is when the document is ready, and not necessarily when it is "loaded".

See here

I use neither. Instead, I depend on YUI's onDomReady() (or onContentReady()/onAvailable()), because it handles the timing of initialization for me.

(Other JS libraries have similar methods for executing only once the page is fully loaded, as this is a mon JS problem.)

That is not conform to any (X)HTML spec and I would be advised against that. It would put your site in to a quirks mode of the browser.

The correct way around the issue would be to use the DOMContentLoaded event, which isn't supported in all browsers. Hacks (eg polling doScroll() or using onreadystatechange) exist, so libraries are able to provide this functionality across browsers.

But there are still problems with DOMContentLoaded and chunked transfers which haven't been addressed by popular JavaScript frameworks.

Here's my take on the issue.

发布评论

评论列表(0)

  1. 暂无评论