


The difference between jquery $(document).ready() and window.onload_jquery
1. Execution time
window.onload must wait until all elements in the page, including images, are loaded before it can be executed.
$(document).ready() is executed after the DOM structure is drawn, without having to wait until it is loaded.
2. The number of writes is different
Window.onload cannot write multiple at the same time. If there are multiple Window.Onload methods, only one
$(document).ready() can be written multiple times at the same time, and all can be executed
3. Simplified writing
There is no simplified writing method for window.onload
$(document).ready(function(){}) can be abbreviated as $(function(){});
In my previous development, I usually used javascript, and I always used jquery mode. That is, most of the time, the first line is:
$(document).ready(function(){
...
});
At this time, you don’t have to wait for all js and images to be loaded before you can execute some methods, but sometimes, you have to wait for all
When all elements are loaded, some methods can be executed. For example, some pictures or other aspects have not been loaded yet. At this time, clicking some buttons will cause unexpected situations. At this time,
Need to use:
$(window).load(function() {
$("#btn-upload").click(function(){ //For example:
uploadPhotos();
});
});
The following is the reproduced content,
Several reasons to use $(window).load(function(){...}) instead of body.onload()
First of all, they are executed after all elements of the page (including html tags and all referenced images, Flash and other media) are loaded. This is what they have in common.
Reason 1 for not using body.Onload():
If we want to load multiple functions at the same time, we must write like this
looks extremely ugly. If we use $(window).load() we can load multiple functions like this$(window).load(function() {
alert("hello, this is jQuery!");
});
$(window).load(function() {
alert("hello, I am also jQuery");
});
Written like this, it will execute these two functions from top to bottom, and it looks much prettier.
Reason 2 for not using body.Onload():
Using body.Onload() cannot completely separate js and html. This is a very serious problem.
In addition, using $(window).load(function(){...}) and body.onload() has the same problem, because as mentioned at the beginning, they both need to wait until all the content of the page is available
It is executed after loading is complete, but if the network speed is relatively slow, it often takes a long time to load a page (ranging from a few seconds to more than ten seconds, or even longer...), so we often
There will be situations where the page has not been fully loaded and the user is already operating the page, so the effect of the page will be different from what we expected,
So here I recommend using $(document).ready(function(){}), or abbreviated as $(function(){}), because it will be executed after the DOM element of the page is loaded,
No need to wait for images or other media to download.
But sometimes we do need to wait until everything on the page is loaded before executing the function we want to execute, so should we use $(window).load(function(){...}) or
The use of $(function(){}) often requires different choices based on specific needs.
Finally, attach a piece of jQuery code that is executed before all DOM elements are loaded
Haha, sometimes we also have this need!
Take the browser loading a document as an example. After the page is loaded, the browser will add events to the DOM elements through Javascript. In regular Javascript code, the window.onload method is usually used, while in Jquery, the $(document).ready() method is used. The $(document).ready() method is the most important function in the event module, which can greatly improve the speed of web applications.
window.load $(document).ready()
Execution timing: You must wait for all content in the webpage to be loaded (including images) before execution. Execution occurs after all DOM structures in the webpage are drawn. It is possible that the content associated with the DOM element has not been loaded.
Number of items to write You cannot write multiple
at the same time
The following code does not execute correctly:
window.onload = function(){ alert(“text1”); }; window.onload = function(){ alert(“text2”); };
Only the second result is output. Multiple
can be written at the same time.
The following code executes correctly:
$(document).ready(function(){ alert(“Hello World”); }); $(document).ready(function(){ alert(“Hello again”); });
The result is output twice
Simplified writing None
$(function(){ // do something });
In addition, it should be noted that since the event registered in the $(document).ready() method will be executed as long as the DOM is ready, the associated file of the element may not be downloaded at this time. For example, the html related to the image has been downloaded and parsed into a DOM tree, but it is very likely that the image has not been loaded yet, so attributes such as the height and width of the image may not be valid at this time. To solve this problem, you can use another page loading method in Jquery --- the load() method. The Load() method binds a handler function to the element's onload event. If the handler function is bound to the window object, it will be triggered after all content (including windows, frames, objects, images, etc.) is loaded. If the handler function is bound to an element, it will be triggered after the content of the element is loaded.
The Jquery code is as follows:
$(window).load(function (){
//Write code
}); Equivalent to the following code in JavaScript
Window.onload = function (){
//Write code
}
————————————————————————————
When I recently changed a page embedded in a frame, I used jquery for effect, and the page itself was also bound to the onload event. After the modification, the test runs normally and smoothly under Firefox, but it takes more than ten seconds for the jquery effect to appear under IE, and the day lily is cold.
At first I thought it was conflicting with the onload method. A common saying on the Internet is that $(document).ready() is executed after the page DOM parsing is completed, and the onload event is executed after all resources are prepared. In other words, $(document).ready() is executed after the page DOM parsing is completed. Executed before onload, especially when the page pictures are larger and more, the time difference may be larger. But on my page, the picture has been displayed for more than ten seconds, but the jquery effect has not yet appeared.
Try deleting the onload loading method, but the result is still the same. It seems that there is no need to use $(document).ready() to write the original onload event binding. So what is the reason why Firefox works but IE does? Then debugging, I found that the originally bound onload method under IE was executed before the content of $(document).ready(), while Firefox executed the content of $(document).ready() first, and then executed the original onload method. . This doesn’t seem to be completely consistent with what’s said online. Haha, it’s interesting. It seems to be getting closer to the truth.
Look through the source code of jquery to see how $(document).ready() is implemented:
if ( jQuery.browser.msie && window == top ) (function(){ if (jQuery.isReady) return; try { document.documentElement.doScroll("left"); } catch( error ) { setTimeout( arguments.callee, 0 ); return; } // and execute any waiting functions jQuery.ready(); })(); jQuery.event.add( window, "load", jQuery.ready );
The result is very clear. Only when the page is not embedded in a frame, IE, like Firefox, first executes the content of $(document).ready(), and then executes the original onload method. For the page embedded in the frame, it is only bound to the load event for execution, so naturally it is the turn after the original onload binding method is executed. And this page happens to have a resource that is inaccessible in the test environment, and the delay of more than ten seconds is exactly the time difference it amplifies.

JavaScript runs in browsers and Node.js environments and relies on the JavaScript engine to parse and execute code. 1) Generate abstract syntax tree (AST) in the parsing stage; 2) convert AST into bytecode or machine code in the compilation stage; 3) execute the compiled code in the execution stage.

The future trends of Python and JavaScript include: 1. Python will consolidate its position in the fields of scientific computing and AI, 2. JavaScript will promote the development of web technology, 3. Cross-platform development will become a hot topic, and 4. Performance optimization will be the focus. Both will continue to expand application scenarios in their respective fields and make more breakthroughs in performance.

Both Python and JavaScript's choices in development environments are important. 1) Python's development environment includes PyCharm, JupyterNotebook and Anaconda, which are suitable for data science and rapid prototyping. 2) The development environment of JavaScript includes Node.js, VSCode and Webpack, which are suitable for front-end and back-end development. Choosing the right tools according to project needs can improve development efficiency and project success rate.

Yes, the engine core of JavaScript is written in C. 1) The C language provides efficient performance and underlying control, which is suitable for the development of JavaScript engine. 2) Taking the V8 engine as an example, its core is written in C, combining the efficiency and object-oriented characteristics of C. 3) The working principle of the JavaScript engine includes parsing, compiling and execution, and the C language plays a key role in these processes.

JavaScript is at the heart of modern websites because it enhances the interactivity and dynamicity of web pages. 1) It allows to change content without refreshing the page, 2) manipulate web pages through DOMAPI, 3) support complex interactive effects such as animation and drag-and-drop, 4) optimize performance and best practices to improve user experience.

C and JavaScript achieve interoperability through WebAssembly. 1) C code is compiled into WebAssembly module and introduced into JavaScript environment to enhance computing power. 2) In game development, C handles physics engines and graphics rendering, and JavaScript is responsible for game logic and user interface.

JavaScript is widely used in websites, mobile applications, desktop applications and server-side programming. 1) In website development, JavaScript operates DOM together with HTML and CSS to achieve dynamic effects and supports frameworks such as jQuery and React. 2) Through ReactNative and Ionic, JavaScript is used to develop cross-platform mobile applications. 3) The Electron framework enables JavaScript to build desktop applications. 4) Node.js allows JavaScript to run on the server side and supports high concurrent requests.

Python is more suitable for data science and automation, while JavaScript is more suitable for front-end and full-stack development. 1. Python performs well in data science and machine learning, using libraries such as NumPy and Pandas for data processing and modeling. 2. Python is concise and efficient in automation and scripting. 3. JavaScript is indispensable in front-end development and is used to build dynamic web pages and single-page applications. 4. JavaScript plays a role in back-end development through Node.js and supports full-stack development.


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

MantisBT
Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.

EditPlus Chinese cracked version
Small size, syntax highlighting, does not support code prompt function

SublimeText3 Chinese version
Chinese version, very easy to use

ZendStudio 13.5.1 Mac
Powerful PHP integrated development environment

SecLists
SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.
