← Back to jQuery Course | Chapter 9: AJAX Basics | Lesson 2 of 7

load() से Content Load करना

load method server से content लाता है और उसे एक selected element के अंदर डाल देता है। यह एक छोटा और simple AJAX method है।
Syntax
javascript
$(selector).load(url, data, function (response, status, xhr) {
  // callback
});

load() क्या है?

load() jQuery का shorthand AJAX method है -- यह किसी URL से HTML fetch करता है और इसे सीधे एक चुने हुए element में insert कर देता है, request और DOM insertion दोनों को एक step में combine करते हुए।

उदाहरण: What is load()?

javascript
<!DOCTYPE html>
<html>
  <head>
    <script src="https://code.jquery.com/jquery-3.7.1.min.js"></script>
  </head>
  <body>
    <div id="content"></div>
    <script>
      $("#content").load("data.php");
    </script>
  </body>
</html>

Callback के साथ Load

load() को pass किया गया एक callback function loading खत्म होने के बाद चलता है, response text, status, और XHR object receive करते हुए। यह एक confirmation दिखाने के लिए उपयोगी है।

उदाहरण: Load with a Callback

javascript
<!DOCTYPE html>
<html>
  <head>
    <script src="https://code.jquery.com/jquery-3.7.1.min.js"></script>
  </head>
  <body>
    <div id="content"></div>
    <script>
      $("#content").load("data.php", function(response, status, xhr) {
      console.log("Load status:", status);
      });
    </script>
  </body>
</html>

अलग-अलग Elements में Load करना

आप किसी भी element को load() की destination बना सकते हैं, जो page के सिर्फ एक section को refresh करना आसान बनाता है -- जैसे comments list या sidebar -- पूरे page को नहीं।

उदाहरण: Load into Different Elements

javascript
<!DOCTYPE html>
<html>
  <head>
    <script src="https://code.jquery.com/jquery-3.7.1.min.js"></script>
  </head>
  <body>
    <div id="sidebar"></div>
    <div id="comments"></div>
    <script>
      $("#comments").load("data.php");
    </script>
  </body>
</html>

Load और Status दिखाना

load() को एक status message के साथ combine करना -- call से पहले 'Loading...' दिखाना, फिर callback में इसे clear करना -- user को content आने पर simple, immediate feedback देता है।

उदाहरण: Load and Show Status

javascript
<!DOCTYPE html>
<html>
  <head>
    <script src="https://code.jquery.com/jquery-3.7.1.min.js"></script>
  </head>
  <body>
    <div id="content">Loading...</div>
    <script>
      $("#content").load("data.php", function() {
      console.log("Load complete");
      });
    </script>
  </body>
</html>

एक छोटा load() Project

load() method तब उपयोगी है जब page के किसी एक हिस्से को fresh content चाहिए, जैसे एक live comment count या किसी दूसरी file से included snippet, जबकि बाकी page वैसा ही रहता है।

उदाहरण: Small load() Project

javascript
<!DOCTYPE html>
<html>
  <head>
    <script src="https://code.jquery.com/jquery-3.7.1.min.js"></script>
  </head>
  <body>
    <span id="commentCount">Loading...</span>
    <script>
      $("#commentCount").load("data.php");
    </script>
  </body>
</html>
Related Topics
{# common_mistakes/chapter_summary/browser_support: on Hindi pages the view already swaps in the hi_ translation fields (or blanks these out if untranslated), so this renders correctly for both languages without a lang_code check here. #}
आम गलतियां
  1. .load() को गलत चीज़ पर इस्तेमाल करना, जैसे $.load("page.html"), जबकि यह एक element method है जिसे एक target चाहिए जैसे $("#content").load("page.html")।
  2. .load() के तुरंत बाद loaded content पढ़ने वाला code चलाना बजाय इसके callback के अंदर, ताकि नए elements अभी exist ही न करें।
  3. "page.html #box" जैसे trailing selector के साथ एक URL pass करना लेकिन space भूल जाना, जो jQuery को सिर्फ उस fragment को load करने के बजाय एक गलत URL request करवा देता है।

Login to run this code

C/C++/Java/PHP execution requires a free account. Your code is saved — you'll land right back in the editor after logging in.