jQuery .load() Method

Beginner
8 min read
Updated: Oct 2026
5 examples
Live preview

What Is .load()?

.load(url [, data] [, complete]) fetches data from the server and places the returned HTML into the matched elements (since jQuery 1.0). It is the simplest jQuery Ajax shorthand for injecting HTML — roughly $.get() plus an automatic .html() update.

Remember
$("#result").load("ajax/test.html");

// With fragment selector (space + selector):
$("#result").load("ajax/test.html #container");

// POST when data is an object; GET otherwise
$("#feeds").load("feeds.php", { limit: 25 }, function () { /* complete */ });

// Empty selection → no Ajax request is sent
NeedUse
Inject HTML into a box$("#box").load(url)
Part of a page only$("#box").load("page.html #part")
Process response yourself$.get()
Full Ajax control$.ajax()
Send form-like data$.post() or .load(url, data)

How to Use .load()

Pick a target element, pass a URL, optionally add data or a complete callback.

  1. Select the container that should receive HTML (it must exist).
  2. Call .load(url) for a GET, or .load(url, data) for a POST.
  3. Optionally append a space + selector to load only a fragment.
  4. Use the complete callback to react after insertion (or on error).
  5. Only load trusted URLs — scripts may run when no fragment selector is used.
PieceRequired?Role
urlYesResource to fetch; may include a fragment selector after a space
dataOptionalObject/string → POST body; omitted → GET
completeOptionalfunction(responseText, textStatus, jqXHR) — once per matched element
Matched elementYesEmpty set → request is never sent

Pseudocode

Pseudocode
call $(selector).load(url [, data] [, complete])

if no elements matched:
    do nothing (no Ajax)
    return

split url on first space → remoteUrl, optionalFragmentSelector
if data is object/string:
    method = POST
else:
    method = GET

send Ajax request for remoteUrl
when response arrives:
    if success or notmodified:
        if fragmentSelector:
            parse HTML, strip scripts, keep matching nodes
        else:
            set .html(response)  // scripts may execute
    if complete provided:
        for each matched element:
            call complete.call(element, responseText, textStatus, jqXHR)

Cheat Sheet

Keep these facts handy while injecting remote HTML.

TopicDetail
Signature.load(url [, data] [, complete])
ReturnsjQuery collection (chainable)
MethodGET by default; POST when data is provided
Fragment"page.html #id" — space + selector
Complete argsresponseText, textStatus, jqXHR
this in completeThe DOM element being updated
ScriptsMay run without selector; stripped with fragment selector
Empty setNo request sent
SincejQuery 1.0

Live Preview

Load real HTML into a panel — full page, fragment only, or a failing URL.

Demo uses public HTTP APIs. Needs network access.

Panel
Ready — pick an action and click Run.

jQuery Examples

Short, copy-ready snippets. Open DevTools Console, or use Try It Yourself for a live editor.

Example 1 — Basic .load(url)

Official one-liner: load remote HTML into a container.

jQuery
$("#result").load("ajax/test.html");

// On click:
$("#run").on("click", function () {
  $("#result").load("https://httpbin.org/html");
});
Try It Yourself

How It Works

  1. Select. Target the element that should receive HTML.
  2. Fetch. jQuery sends a GET for the URL.
  3. Insert. On success, the response becomes the element’s HTML.
  4. Empty set. If nothing matched, no request is sent.

Example 2 — Load a Page Fragment

Space + selector keeps only part of the fetched document.

jQuery
$("#result").load("ajax/test.html #container");

// Live demo style:
$("#snippet").load("https://httpbin.org/html h1");
Try It Yourself

How It Works

  1. Split. Text after the first space is treated as a selector.
  2. Parse. jQuery parses the response with browser HTML rules.
  3. Keep. Only matching nodes are inserted; the rest is discarded.
  4. Scripts. With a fragment selector, scripts are stripped and do not run.

Example 3 — Complete Callback

Run code after insertion — this is the updated DOM element.

jQuery
$("#result").load("ajax/test.html", function () {
  alert("Load was performed.");
});

$("#panel").load("https://httpbin.org/html h1", function (responseText, status, jqXHR) {
  $("#meta").text("Status: " + status + " | HTTP " + jqXHR.status);
});
Try It Yourself

How It Works

  1. Order. Complete runs after HTML insertion (on success).
  2. Args. You get responseText, textStatus, and jqXHR.
  3. Context. this is the element being updated.
  4. Once each. The callback fires once per matched element.

Example 4 — Error Handling in Complete

Official pattern: check status === "error" and show a message.

jQuery
$("#success").load("/not-here.php", function (response, status, xhr) {
  if (status === "error") {
    var msg = "Sorry but there was an error: ";
    $("#error").html(msg + xhr.status + " " + xhr.statusText);
  }
});
Try It Yourself

How It Works

  1. Always. Complete runs for success and failure.
  2. Check. Inspect textStatus for "error".
  3. Report. Use xhr.status and xhr.statusText.
  4. UX. Show errors in a dedicated element so users notice the failure.

Example 5 — POST with a Data Object

Passing data switches the request to POST and sends the payload.

jQuery
$("#feeds").load("feeds.php", { limit: 25 }, function () {
  alert("The last 25 entries in the feed have been loaded");
});

// Arrays in the POST body:
$("#objectID").load("test.php", { "choices[]": ["Jon", "Susan"] });
Try It Yourself

How It Works

  1. Switch. A data object/string makes .load() use POST.
  2. Serialize. Data is sent in the request body (via $.param()).
  3. Insert. Returned HTML replaces the target contents on success.
  4. Callback. The third argument is complete — not a jqXHR chain.

Edge Cases & Pitfalls

Check these before shipping partial HTML loads.

empty

Empty selection sends nothing

If the target selector matches no elements, jQuery skips the Ajax call entirely.

scripts

Scripts may execute without a fragment

Full-page loads can run script blocks. Fragment loads strip scripts. Only load trusted URLs.

cors

Same-origin policy applies

Cross-origin loads need CORS (or a same-origin proxy). Most browsers block plain cross-domain HTML fetches.

parse

Browsers filter markup while parsing

html, head, and body tags may be dropped during innerHTML parsing — inserted markup can differ from a full page view.

Browser Support

.load() has been part of jQuery since 1.0. It works wherever your loaded jQuery version can run Ajax. Cross-origin HTML still needs CORS. Prefer a current jQuery 3.x build for modern projects.

✓ Stable · jQuery 1.0+

jQuery .load()

Works in every browser that runs your loaded jQuery version. Same-origin (or CORS-enabled) HTML endpoints required.

100% With jQuery Ajax
Google Chrome All versions · Desktop & Mobile
Full support
Mozilla Firefox All versions · Desktop & Mobile
Full support
Apple Safari All versions · macOS & iOS
Full support
Microsoft Edge All versions · Chromium & Legacy
Full support
Internet Explorer IE 6+ · Legacy environments
Full support
Opera All modern versions
Full support
.load() Universal

Bottom line: Safe for everyday same-origin partials. Use fragment selectors for safer inserts, and always handle the complete status for errors.

Performance Notes

ApproachCostBest for
Fragment .load(url #part)MediumReuse full pages but insert a slice
Dedicated HTML partialCheaperSmall, purpose-built snippets
Lazy load on first openBest UXTabs, modals, drawers

Fragment loads still download the whole remote document, then filter it. Prefer small server partials when you control the backend.

Key Takeaways

  • Role: simplest way to fetch HTML and inject it into matched elements.
  • Method: GET by default; POST when you pass data.
  • Fragment: "url #selector" inserts a slice; scripts are stripped.
  • Safety: empty targets skip Ajax; only load trusted HTML.

One line: .load() is jQuery’s Ajax shorthand for “get HTML and put it here.”

Frequently Asked Questions

jQuery .load() is a collection method that fetches HTML from a URL and inserts it into each matched element. Call $("#panel").load("/partial.html") — on success jQuery sets the element’s HTML to the response. It is roughly equivalent to $.get() plus .html(), with an implicit success handler. Returns the jQuery collection for chaining.
$.get() is a global function that returns jqXHR — you update the DOM yourself. .load() is a method on a selection that injects returned HTML automatically. .load() also supports a URL fragment selector ("page.html #sidebar") that $.get() does not. Use .load() to inject HTML; use $.get() when you need to process the response yourself.
If you pass a data argument (plain object or string), jQuery uses POST and sends that data in the request body. With only a URL (and optional complete callback), .load() uses GET.
If the url string contains a space, the part after the first space is a jQuery selector applied to the fetched document. $("#result").load("article.html #comments") fetches article.html, extracts #comments, and inserts that into #result. Scripts in fragment loads are stripped and not executed.
Without a selector suffix, scripts pass through .html() and may execute before being discarded. With a selector suffix ("url #fragment"), scripts are stripped before insertion and do not run. Never .load() untrusted URLs — executed scripts have full page privileges.
If the jQuery collection is empty — for example $("#missing").load(url) when #missing does not exist — the Ajax request is not sent at all.

Next: jQuery.param()

Serialize objects and arrays for Ajax query strings and POST bodies.

jQuery.param() tutorial →

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

9 people found this page helpful