what exactly document-ready means in jquery

瘦欲@ 提交于 2019-12-19 03:57:46

问题


Let us say I have a HTML page that contains a javascript file:

The base.js is like this:

$(document).ready(function () {
   obj.init();
}

// ..............

var obj = {...};

Surprisingly, sometimes(not all the time) Firebug shows me the undefined error on obj.init() call! My understanding is that document ready means all html elements, including images, javascript files downloaded and executed(?).

I believe in order to find the root cause of this error, we need understand what exactly the "document ready" means? anybody has any insight?

============================

Update: maybe I should not mention about image over here, my main concern is regarding the javascript file in particular. Does "DOM fully constructed" include "all javascript code executed"?

============================

Update again: It seems people here agreed that event "document.ready" WILL NOT be triggered until ALL javascript code got downloaded and executed. Thus, root cause of the issue remain unknown. I did bypassed this issue after I moved the $(document).ready block to the bottom of the javascript file.


回答1:


From the documentation of jQuery.ready():

While JavaScript provides the load event for executing code when a page is rendered, this event does not get triggered until all assets such as images have been completely received. In most cases, the script can be run as soon as the DOM hierarchy has been fully constructed. The handler passed to .ready() is guaranteed to be executed after the DOM is ready, so this is usually the best place to attach all other event handlers and run other jQuery code. When using scripts that rely on the value of CSS style properties, it's important to reference external stylesheets or embed style elements before referencing the scripts.

In cases where code relies on loaded assets (for example, if the dimensions of an image are required), the code should be placed in a handler for the load event instead.




回答2:


The ready event happens when the document is loaded and parsed.

This includes all Javascript files, but not images.

The ready event happens after the document is parsed. Some browsers have a specific event for this, in other browsers jQuery uses a timer that polls the status of the document. This means that the event happens either as soon as the entire document is parsed, or slightly later, depending on the browser. This is normally not a problem, as it doesn't happen before anything of the document is parsed.

If you need all images to be loaded before you do something, you should use the load event instead.




回答3:


You should define obj before referencing it.

Also, document.ready doesn't mean that the resources will be loaded, only that the document been parsed, so the resources load between document ready and the $(window).load event.



来源:https://stackoverflow.com/questions/9504315/what-exactly-document-ready-means-in-jquery

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!