The jQuery .ready() method runs your code when the DOM is parsed and safe to manipulate. This tutorial covers the recommended $(function(){}) shorthand, handler ordering, comparisons with DOMContentLoaded and window load, the jQuery.noConflict() pattern, and how .ready() relates to jQuery.ready.
01
$(fn)
Shorthand
02
DOM safe
Parse done
03
Queue
In order
04
Late bind
Still runs
05
Not load
Before imgs
06
3.0+
$(fn) only
Fundamentals
Introduction
jQuery scripts usually need HTML elements to exist before they can select nodes, attach events, or change the page. If your script runs in the <head> before the body markup appears, $("#menu") finds nothing.
The .ready( handler ) method solves that by queueing your function until jQuery detects the document is ready — the same timing as the browser’s DOMContentLoaded event, but with jQuery’s reliable late-registration behavior.
Most developers never write .ready explicitly. They use the shorthand $(function(){ ... }) — the pattern you will see in virtually every jQuery tutorial and plugin.
Concept
Understanding the .ready() Method
.ready( handler ) accepts one argument: a function with no parameters. jQuery calls it when the DOM is ready. The method returns the same jQuery object it was called on (usually jQuery(document) or the collection from $(fn)), so you can chain if needed.
Handlers run in the order they were registered. Since jQuery 3.0, if one handler throws an error, later handlers still execute — jQuery isolates exceptions so one bad plugin does not break the entire page.
💡
Beginner Tip
Put page initialization inside $(function(){ ... }). Use $(window).on("load", fn) only when you need images or other assets fully downloaded first.
Foundation
📝 Syntax
Register a callback for document ready:
jQuery
.ready( handler )
Recommended forms (jQuery 3.0+)
jQuery
// Recommended shorthand
$(function () {
// Handler for .ready() called.
});
// Equivalent long form
$(document).ready(function () {
// Same timing as above.
});
Parameters
handler (Function) — callback executed when the DOM is ready.
Return value
jQuery object — the collection .ready() was invoked on.
Deprecated equivalents (avoid in new code)
$("img").ready(fn) — misleading; does not wait for images.
$("document").ready(fn) — selects nothing; still worked historically.
$(document).on("ready", fn) — removed in jQuery 3.0.
Cheat Sheet
⚡ Quick Reference
Expression
Meaning
$(fn)
Recommended ready shorthand (3.0+)
$(document).ready(fn)
Explicit equivalent
$(window).on("load", fn)
All assets loaded — different event
document.addEventListener("DOMContentLoaded", fn)
Native — misses late registration
$.when($.ready).then(fn)
Promise-style — see jQuery.ready
Multiple $(fn) calls
Run in registration order
Compare
📋 .ready() vs DOMContentLoaded vs load
Three timing hooks — pick the one that matches what your code needs.
jQuery waits until elements exist, then runs your callback. The paragraph is in the HTML before the script — by ready time, jQuery can select and update it safely.
📈 Practical Patterns
Long form syntax, handler queues, noConflict, and load timing.
Example 2 — $(document).ready() Long Form
Explicit document binding — identical timing to the shorthand.
jQuery
$(document).ready(function () {
// Handler for .ready() called.
$("#status").text("Ready via $(document).ready()");
});
First ready handler
Second ready handler
Third ready handler
How It Works
Plugins and app code can each register their own ready block. jQuery flushes the queue once — order matches registration, which keeps init predictable across files.
Example 4 — Official jQuery.noConflict() Pattern
Ready handler receives $ as first argument when using an aliased jQuery object.
jQuery
var jq2 = jQuery.noConflict();
jq2(function ($) {
// Code using $ as usual; actual jQuery object is jq2
$("p").text("Ready with noConflict — $ works inside handler");
});
When global $ belongs to another library, pass a function to jq2. jQuery injects a local $ parameter scoped to your ready callback — official docs pattern for coexistence.
Example 5 — .ready() vs $(window).on("load")
Ready fires before images finish — use load when you need image dimensions.
jQuery
$(function () {
var img = $("#hero")[0];
console.log("Ready — img width:", img.width); // often 0
});
$(window).on("load", function () {
var img = $("#hero")[0];
console.log("Load — img width:", img.width); // natural width
});
The official docs warn: do not attach a load listener inside ready expecting it to fire if load already passed. For dynamically injected scripts, ready handlers still run; window load may have already occurred.
Applications
🚀 Common Use Cases
Page initialization — bind events, cache DOM references, run plugins.
Progressive enhancement — enhance HTML that already rendered without JS.
Plugin bootstrapping — each file registers $(fn) independently.
Form validation UI — attach submit handlers after inputs exist.
Modern vanilla alternative: DOMContentLoaded or defer on script tags.
Compatibility
Browser Support
.ready() works wherever jQuery runs. jQuery normalizes ready timing across browsers, including legacy IE behavior before DOMContentLoaded was universal.
✓ jQuery 1.0+
jQuery .ready()
Supported in jQuery 1.x, 2.x, and 3.x. Native DOMContentLoaded is available in all modern browsers for non-jQuery code.
100%With jQuery loaded
Google ChromeAll versions · Desktop & Mobile
Full support
Mozilla FirefoxAll versions · Desktop & Mobile
Full support
Apple SafariAll versions · macOS & iOS
Full support
Microsoft EdgeAll versions · Chromium & Legacy
Full support
Internet ExplorerIE 6+ · Legacy environments
Full support
OperaAll modern versions
Full support
.ready()Universal
Bottom line: Default entry point for jQuery pages — wrap DOM work in $(function(){}) unless you need window load or explicit promise composition.
Wrap Up
Conclusion
The jQuery .ready() method is how almost every jQuery page starts — queue a function that runs when the DOM is safe to touch. Use $(function(){}) for everyday work; reach for $(window).on("load") when assets must finish first.
Use delegated events for content loaded later via Ajax
Keep ready handlers focused — split large inits into functions
Use $(window).on("load") for image-dependent layout
Use jq2(function($){}) after noConflict()
❌ Don’t
Manipulate DOM before elements exist without ready
Use deprecated $("img").ready(fn) in new code
Assume ready waits for images or fonts
Attach window load inside ready expecting it always fires
Put heavy blocking work in ready without deferring
Summary
Key Takeaways
Knowledge Unlocked
Five things to remember about jQuery .ready()
Run code when the DOM is parsed — not when all assets load.
5
Core concepts
✅01
$(fn)
Shorthand
Daily
DOM02
Parsed
Not load
Timing
1→303
Order
FIFO queue
Queue
⏱04
Late
Still runs
Bind
3.005
$(fn)
Recommended
Modern
❓ Frequently Asked Questions
.ready(handler) registers a function to run when the DOM is safe to manipulate — after HTML is parsed but before all images and assets finish loading. Returns the jQuery collection for chaining. Available since jQuery 1.0.
They are equivalent. $(fn) is shorthand for passing a function to jQuery, which internally binds it as a document ready handler. Since jQuery 3.0, $(handler) is the only recommended syntax — other forms like $('img').ready(fn) still work but are deprecated.
Both fire when the DOM is parsed. jQuery's .ready() still runs your handler if you register it after DOMContentLoaded already fired — native addEventListener('DOMContentLoaded') added too late never runs. That late-binding behavior is a key jQuery advantage.
Use .ready() (or $(fn)) for DOM setup, event handlers, and plugins. Use $(window).on('load', fn) when you need fully loaded assets — image dimensions, fonts, or layout that depends on resources. DOM ready always happens before window load.
.ready(handler) queues callbacks on jQuery's internal ready system. jQuery.ready is the thenable property that resolves at the same moment — use $.when($.ready, ajax) for promise composition; use $(fn) for everyday page initialization.
Yes. Each .ready() or $(fn) call adds another handler. They execute in registration order when the DOM becomes ready. Since jQuery 3.0, an exception in one handler does not block subsequent handlers from running.
Did you know?
Passing a function directly to jQuery — $(fn) — is not magic syntax. jQuery checks whether the first argument is a function and, if so, treats it as a ready handler rather than a selector string. That is why $(function(){}) works even though no elements are selected. jQuery 3.0 narrowed official recommendations to this form because variants like $("img").ready(fn) confused beginners into thinking ready waited for images to load.