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

getScript() से Scripts Load करना

getScript method किसी URL से एक JavaScript file load करता है और उसे चलाता है। ज़रूरत पड़ने पर extra scripts load करने के लिए यह उपयोगी है।
Syntax
javascript
$.getScript(url, function () {
  // script loaded
});

getScript() क्या है?

$.getScript() AJAX के through एक URL से एक JavaScript file fetch करता है और आते ही उसे execute कर देता है, effectively एक script tag को dynamically inject और run करते हुए।

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

javascript
<!DOCTYPE html>
<html>
  <head>
    <script src="https://code.jquery.com/jquery-3.7.1.min.js"></script>
  </head>
  <body>
    <script>
      $.getScript("https://code.jquery.com/jquery-3.7.1.min.js", function() {
      console.log("Script loaded and executed");
      });
    </script>
  </body>
</html>

एक Success Callback इस्तेमाल करना

done callback script के successfully load और execute हो जाने के बाद चलता है, जो नई script द्वारा define किए गए किसी भी functions को call करना शुरू करने के लिए safe जगह है।

उदाहरण: Use a Success Callback

javascript
<!DOCTYPE html>
<html>
  <head>
    <script src="https://code.jquery.com/jquery-3.7.1.min.js"></script>
  </head>
  <body>
    <script>
      $.getScript("https://code.jquery.com/jquery-3.7.1.min.js").done(function() {
      console.log("Safe to use functions from the new script now");
      });
    </script>
  </body>
</html>

Script Errors Handle करना

एक script load होने में fail हो सकती है -- एक bad URL, network error, या blocked request की वजह से -- और .fail() chain करना आपको उस case को handle करने और एक useful message दिखाने देता है।

उदाहरण: Handle Script Errors

javascript
<!DOCTYPE html>
<html>
  <head>
    <script src="https://code.jquery.com/jquery-3.7.1.min.js"></script>
  </head>
  <body>
    <script>
      $.getScript("https://example.com/missing-script-404.js").fail(function() {
      console.log("Script failed to load");
      });
    </script>
  </body>
</html>

जब ज़रूरत हो तब एक Script Load करना

किसी script को हर page के initial load में शामिल करने के बजाय सिर्फ तभी load करना जब वह असल में ज़रूरी हो, on demand loading कहलाता है, और यह initial page को हल्का रख सकता है।

उदाहरण: Load a Script on Demand

javascript
<!DOCTYPE html>
<html>
  <head>
    <script src="https://code.jquery.com/jquery-3.7.1.min.js"></script>
  </head>
  <body>
    <button id="btn">Load Feature</button>
    <script>
      $("#btn").on("click", function() {
      $.getScript("https://code.jquery.com/jquery-3.7.1.min.js", function() {
      console.log("Feature script loaded on demand");
      });
      });
    </script>
  </body>
</html>

एक छोटा getScript() Project

getScript() तब उपयोगी है जब किसी particular feature के लिए सिर्फ एक extra JavaScript library या plugin चाहिए, जैसे कोई chart library जो सिर्फ एक page पर इस्तेमाल होती है।

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

javascript
<!DOCTYPE html>
<html>
  <head>
    <script src="https://code.jquery.com/jquery-3.7.1.min.js"></script>
  </head>
  <body>
    <button id="loadChart">Load Chart Library</button>
    <script>
      $("#loadChart").on("click", function() {
      $.getScript("https://code.jquery.com/jquery-3.7.1.min.js", function() {
      console.log("Chart library ready");
      });
      });
    </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. किसी script के functions को $.getScript(...) के तुरंत बाद इस्तेमाल करना बजाय उसके .done() callback के अंदर, ताकि वे अभी define ही न हुए हों।
  2. .fail() भूल जाना, ताकि एक गलत URL या blocked request बिना किसी visible feedback के fail हो जाए।
  3. हर click पर बार-बार same script load करना, जो इसे फिर से execute कर देता है और event handlers तथा side effects को duplicate कर सकता है।

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.