search
HomeWeb Front-endJS TutorialDetailed explanation of jQuery's ready method usage

Detailed explanation of jQuery's ready method usage

Jun 25, 2017 am 10:14 AM
jquerymethodusageDetailed explanation

This article mainly introduces how to use the ready method of jQuery. This method is used to achieve the effect of being executed only after the man is loaded. Friends who have the same needs can refer to it.

The ready method in jQuery implements the effect of being executed only after the page is loaded, but it is not a package of window.onload or document.onload, but uses the standard W3C browser DOM to hide the api and IE browser Defects to complete, first, let's look at the jQuery code

The code is as follows:

DOMContentLoaded = function() 
 { 
         //取消
事件
监听,执行ready方法     if ( 
document
.addEventListener ) 
    {      
        document.removeEventListener( "DOMContentLoaded", DOMContentLoaded, false ); 
        jQuery.ready(); 
    } 
     else if ( document.readyState === "complete" )  
    { 
        document.detachEvent( "onreadystatechange", DOMContentLoaded ); 
        jQuery.ready(); 
    } 
};

The code is as follows:

jQuery.ready.promise = function( obj ) { 
    if ( !readyList ) { 
        readyList = jQuery.Deferred(); 
            //表示页面已经加载完成,直接调用 ready方法         if ( document.readyState === "complete" ) {  
            //将 jQuery.ready压入异步消息队列,设置延迟时间1毫秒(注意,有些浏览器延迟不能小于4毫秒)             setTimeout( jQuery.ready);  
        }  
        else if ( document.addEventListener ) //         { 
             //监听DOM加载完成             document.addEventListener( "DOMContentLoaded", DOMContentLoaded, false ); 
             //这里是为了确保所有ready执行结束,如果DOMContentLoaded方法执行了,将有一个状态值 isReady被设置为true,因此,              //ready方法一旦执行,那么将只执行一次,window.addEventListener中的ready 将被 return 中断             window.addEventListener( "load", jQuery.ready, false ); 
        } else { 
            //低版本的IE浏览器             document.attachEvent( "onreadystatechange", DOMContentLoaded ); 
            window.attachEvent( "onload", jQuery.ready ); 
            var top = false; 
            try { 
                top = window.frameElement == null && document.documentElement; 
            } catch(e) {} 
            if ( top && top.doScroll )  //剔除iframe的成分             { 
                (function doScrollCheck() { 
                    if ( !jQuery.isReady ) { 
                        try { 
                            //根据bug来兼容低版本的IE http://
javascript
.nwbox.com/IEContentLoaded/                             top.doScroll("left"); 
                        } catch(e) { 
                            //由于低版本的IE 浏览器,onreadystatechange事件不可靠,因此需要根据各个bug来判断页面是否已加载完成                             return setTimeout( doScrollCheck, 50 );  
                        } 
                        jQuery.ready(); 
                    } 
                })(); 
            } 
        } 
    } 
    return readyList.promise( obj ); 
};

The code is as follows:

ready: function( wait )
 {
 if ( wait === true ? --jQuery.readyWait : jQuery.isReady ) { 
  //判断页面是否已完成加载并且是否已经执行ready方法
  return;
 }
 if ( !document.body ) {
  return setTimeout( jQuery.ready );
 }
 jQuery.isReady = true; //指示ready方法已被执行
 if ( wait !== true && --jQuery.readyWait > 0 ) {
  return;
 }
 readyList.resolveWith( document, [ jQuery ] );
 if ( jQuery.fn.trigger ) {
  jQuery( document ).trigger("ready").off("ready"); 
 }
},

Summary:

There are two events when the page is loaded. One is ready, which indicates that the document structure has been loaded (excluding non-text media files such as pictures), and the other It is onload, indicating that all elements of the page including pictures and other files have been loaded. (It can be said: ready is loaded before onload!!!)
General style control, such as image size control, is loaded in onload;
The jS event triggering method can be loaded in ready;

The above is the detailed content of Detailed explanation of jQuery's ready method usage. For more information, please follow other related articles on the PHP Chinese website!

Statement
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Python vs. JavaScript: Choosing the Right Tool for the JobPython vs. JavaScript: Choosing the Right Tool for the JobMay 08, 2025 am 12:10 AM

Whether to choose Python or JavaScript depends on the project type: 1) Choose Python for data science and automation tasks; 2) Choose JavaScript for front-end and full-stack development. Python is favored for its powerful library in data processing and automation, while JavaScript is indispensable for its advantages in web interaction and full-stack development.

Python and JavaScript: Understanding the Strengths of EachPython and JavaScript: Understanding the Strengths of EachMay 06, 2025 am 12:15 AM

Python and JavaScript each have their own advantages, and the choice depends on project needs and personal preferences. 1. Python is easy to learn, with concise syntax, suitable for data science and back-end development, but has a slow execution speed. 2. JavaScript is everywhere in front-end development and has strong asynchronous programming capabilities. Node.js makes it suitable for full-stack development, but the syntax may be complex and error-prone.

JavaScript's Core: Is It Built on C or C  ?JavaScript's Core: Is It Built on C or C ?May 05, 2025 am 12:07 AM

JavaScriptisnotbuiltonCorC ;it'saninterpretedlanguagethatrunsonenginesoftenwritteninC .1)JavaScriptwasdesignedasalightweight,interpretedlanguageforwebbrowsers.2)EnginesevolvedfromsimpleinterpreterstoJITcompilers,typicallyinC ,improvingperformance.

JavaScript Applications: From Front-End to Back-EndJavaScript Applications: From Front-End to Back-EndMay 04, 2025 am 12:12 AM

JavaScript can be used for front-end and back-end development. The front-end enhances the user experience through DOM operations, and the back-end handles server tasks through Node.js. 1. Front-end example: Change the content of the web page text. 2. Backend example: Create a Node.js server.

Python vs. JavaScript: Which Language Should You Learn?Python vs. JavaScript: Which Language Should You Learn?May 03, 2025 am 12:10 AM

Choosing Python or JavaScript should be based on career development, learning curve and ecosystem: 1) Career development: Python is suitable for data science and back-end development, while JavaScript is suitable for front-end and full-stack development. 2) Learning curve: Python syntax is concise and suitable for beginners; JavaScript syntax is flexible. 3) Ecosystem: Python has rich scientific computing libraries, and JavaScript has a powerful front-end framework.

JavaScript Frameworks: Powering Modern Web DevelopmentJavaScript Frameworks: Powering Modern Web DevelopmentMay 02, 2025 am 12:04 AM

The power of the JavaScript framework lies in simplifying development, improving user experience and application performance. When choosing a framework, consider: 1. Project size and complexity, 2. Team experience, 3. Ecosystem and community support.

The Relationship Between JavaScript, C  , and BrowsersThe Relationship Between JavaScript, C , and BrowsersMay 01, 2025 am 12:06 AM

Introduction I know you may find it strange, what exactly does JavaScript, C and browser have to do? They seem to be unrelated, but in fact, they play a very important role in modern web development. Today we will discuss the close connection between these three. Through this article, you will learn how JavaScript runs in the browser, the role of C in the browser engine, and how they work together to drive rendering and interaction of web pages. We all know the relationship between JavaScript and browser. JavaScript is the core language of front-end development. It runs directly in the browser, making web pages vivid and interesting. Have you ever wondered why JavaScr

Node.js Streams with TypeScriptNode.js Streams with TypeScriptApr 30, 2025 am 08:22 AM

Node.js excels at efficient I/O, largely thanks to streams. Streams process data incrementally, avoiding memory overload—ideal for large files, network tasks, and real-time applications. Combining streams with TypeScript's type safety creates a powe

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

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

Hot Tools

SecLists

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.

SAP NetWeaver Server Adapter for Eclipse

SAP NetWeaver Server Adapter for Eclipse

Integrate Eclipse with SAP NetWeaver application server.

Atom editor mac version download

Atom editor mac version download

The most popular open source editor

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

WebStorm Mac version

WebStorm Mac version

Useful JavaScript development tools