Do project to use the load mechanism to dynamically load JS, due to file dependencies, requires JS must be loaded one by one (that is, sync), file loading no problem, but found that there is a problem when in $ (document). Ready () Using the document.write form of load will result in page whitespace, view the DOM, and discover that it has been emptied.After checking Google knows that because document.write only when the DOM is not
Window.onload must wait until all the elements of the page that include the picture have been loaded before they can be executed.$ (document). Ready () is executed when the DOM structure is drawn and does not have to wait for the load to complete.Window.onload cannot write multiple at the same time, if there are multiple window.onload methods, only one$ (document). Ready () can be written multiple at the sa
This article mainly introduces windows. onload () and $ (document ). ready () difference after the browser loads the DOM, it adds events to the DOM element through javascript. In javascript, window is usually used. onload () method.
In jquery, the $ (document). ready () method is used. The following describes the differences between the two.
Window. onload ()
$ (Document).
This article mainly introduces window. onload and $ (document ). the difference between ready () and the example analyzes the differences between the two in the use of page elements. For more information, see the example in this article. onload and $ (document ). ready. Share it with you for your reference. The specific analysis is as follows:
Window. onload is a function in Javascript, meaning: Wait for
1, window.onload in $ (document). Ready () after executionCause: Both trigger or not, are based on the document.readystate attribute.(1) Value of document.readystate: Uninitialized, loading, interactive, complete (these four values, sorted by time).(2) When its value is interactive, the documentcontentloaded event is triggered, and Jquery.ready () is a function designed for the documentcontentloaded event. This executes the task queue defined by the J
Onload needs to be executed after all resources on the page are loaded, while ready is when the DOM document tree has been parsed, ready is faster than onload. For example, a large image is displayed on a page, which takes a long time to load. onload is executed only after the image is loaded, while ready does not have to wait until the image is loaded.
In $ (do
The difference between the ready function of jquery and the onload of JS:1. Execution timeWindow.onload must wait until all the elements of the page that include the picture have been loaded before they can be executed.$ (document). Ready () is executed when the DOM structure is drawn and does not have to wait for the load to complete.2. Different writing numbersWindow.onload cannot write multiple at the sa
jquery Ready () bind () toggle () addclass () event handlingA function of the more commonly used dynamic effects provided in jquery. You can also add Parameters: Show (Speed,[callback]) displays all matching elements in an elegant animation, and optionally triggers a callback function when the display is complete.
Ready (FN)
Code:
JS Code$ (document). Ready
Look at the code written before, there are many such code:
Copy Code code as follows:
$ (function () {
Do something.
});
There may be some initialization styles written here, additional event handling for other DOM elements, and so on. I didn't find anything wrong at first. However, when the page references some slow downloading pictures, the problem arises: after the page HTML is downloaded, the events bound by the binding DOM elements cannot be executed, and the sty
$ (document). Ready () and window.onload on the form, we go to execute a function or action, but in the exact details, $ (document) And window.onload still have a difference, let me introduce the following.The most basic difference1. Execution timeWindow.onload must wait until all the elements of the page that include the picture have been loaded before they can be executed. $ (document). Ready () is execut
1. Execution Time Window.onload must wait until all the elements of the page that include the picture have been loaded before they can be executed. $ (document). Ready () is executed when the DOM structure is drawn and does not have to wait for the load to complete. 2. different writing numbers window.onloadNBSP; $ (document). Ready () can be written multiple at the same time and can be executed3.
jquery $ (document). Ready () method and Window.onload methodIn most cases, the jquery code is written that requires us to put the jquery code in the
$ (document). Ready (function () {
... jquery code ........ .....
});
The above code and
$ (). Ready (function () {
... jquery code ........ .....
});$ (function () {
... jquery code ........ .....
});
is
In jquery, we can see two ways of writing: $ (function () {}) and $ (document). Ready (function () {})Both of these methods have the same effect, which is to execute a function after the DOM document tree has been loaded (note that the document tree inside does not load all of the files).Instead, Window.onload executes a function after the DOM document tree has been loaded and all files have been loaded. That is, $ (document).
$ (document). Ready (...) and window.onload: events have a similar effect, but there are subtle differences in the time they are triggered.1.window.onload:The Window.onload event is triggered when a document is completely downloaded to the browser. This means that all the elements on the page are accessible to JavaScript, which is advantageous for writing functional code because there is no need to consider the order of loading.2.$ (document).
The first thing to learn about jquery is that if you want an event to run on your page, you must call this event in the (document). Ready (). All elements or events that are included in the. Ready () will load immediately after the DOM finishes loading and before the page content is loaded.If you want a event to work on your page, you should call it inside the $ (document).
Recently, when I changed a page embedded in the frame, jquery was used for the effect, and the page itself was bound with the onload event. After the change, the test in Firefox is normal and smooth. in IE, it takes more than a dozen seconds for jquery to show up, and daylily is getting cooler.
At first, we thought it was a conflict with the onload loading method. $ (Document) is widely used on the Internet ). ready () is executed after the DOM pa
Jqurey event ready method usage, jqureyready
Ready activates the function after the document is loaded.
Example:
Definition and usage:
When the DOM (Document Object Model) has been loaded and the page (including images) has been fully rendered, a ready event occurs.
Since this event occurs after the document is ready
There are two types of page loading events: ready, indicating that the file structure has been loaded (excluding non-text media files such as images), and onload, indicates that all elements, including images, are loaded. (You can say: ready loads before onload !!!)Generally, the style is controlled, and the slice size is controlled to be loaded in onload;The jS event trigger method can be loaded in
There are two types of page loading events: ready, indicating that the file structure has been loaded (excluding non-text media files such as images), and onload, indicates that all elements, including images, are loaded. (You can say: ready loads before onload !!!)
Generally, the style is controlled, and the slice size is controlled to be loaded in onload;
The jS event trigger method can be loaded in
The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion;
products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the
content of the page makes you feel confusing, please write us an email, we will handle the problem
within 5 days after receiving your email.
If you find any instances of plagiarism from the community, please send an email to:
info-contact@alibabacloud.com
and provide relevant evidence. A staff member will contact you within 5 working days.