← Back to jQuery Course | Chapter 1: Getting Started | Lesson 2 of 6

किसी Webpage में jQuery जोड़ना

आप एक script element जोड़कर एक CDN से jQuery load कर सकते हैं।
Syntax
javascript
<script src="https://code.jquery.com/jquery-version.min.js"></script>

CDN Script जोड़ना

किसी public CDN URL की तरफ़ point करने वाला एक script tag आपके page के चलने से पहले jQuery library file download कर लेता है, बिना किसी local install की ज़रूरत के। शुरुआत करने का यह सबसे तेज़ तरीका है, क्योंकि locally download या configure करने के लिए कुछ नहीं है।

उदाहरण: Add the CDN Script

javascript
<!DOCTYPE html>
<html>
  <head>
    <script src="https://code.jquery.com/jquery-3.7.1.min.js"></script>
  </head>
  <body>
  </body>
</html>

jQuery Code चलाना

एक बार जब CDN script tag ने jQuery को page में load कर दिया, तो global $ function बाद में चलने वाले किसी भी script के लिए available हो जाता है। elements select करने के लिए $() इस्तेमाल करना वह पहली चीज़ है जो ज़्यादातर developers यह confirm करने के लिए try करते हैं कि library सही तरीके से load हुई।

उदाहरण: Run jQuery Code

javascript
<!DOCTYPE html>
<html>
  <head>
    <script src="https://code.jquery.com/jquery-3.7.1.min.js"></script>
  </head>
  <body>
    <script>
      console.log($("body").length);
    </script>
  </body>
</html>

अपना Script रखना

आपका अपना script tag HTML में jQuery CDN tag के बाद आना चाहिए, वरना जैसे ही आपका code चलेगा browser एक '$ is not defined' error throw कर देगा। load order सही रखना CDN-based setups के साथ सबसे common early mistakes में से एक है।

उदाहरण: Place Your Script

javascript
<!DOCTYPE html>
<html>
  <head>
    <script src="https://code.jquery.com/jquery-3.7.1.min.js"></script>
    <script>
      console.log($.fn.jquery);
    </script>
  </head>
  <body></body>
</html>

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

किसी button को एक click handler से जोड़ना यह साबित करने का एक तेज़ तरीका है कि पूरा setup end-to-end काम करता है -- अगर आपके click करने पर console एक message log करे, तो CDN load और आपका script दोनों सही तरीके से जुड़े हुए हैं।

उदाहरण: Use a Button

javascript
<!DOCTYPE html>
<html>
  <head>
    <script src="https://code.jquery.com/jquery-3.7.1.min.js"></script>
  </head>
  <body>
    <button id="test">Test</button>
    <script>
      $("#test").click(function() {
      console.log("jQuery is working");
      });
    </script>
  </body>
</html>

यह Check करना कि jQuery Load हुआ

console में jQuery.fn.jquery या $.fn.jquery log करना loaded library की exact version string print करता है, जो यह confirm करने के लिए उपयोगी है कि आपको CDN से expected version मिला, खासतौर पर किसी compatibility issue को debug करते समय।

उदाहरण: Check That jQuery Loaded

javascript
<!DOCTYPE html>
<html>
  <head>
    <script src="https://code.jquery.com/jquery-3.7.1.min.js"></script>
  </head>
  <body>
    <script>
      console.log($.fn.jquery);
      console.log(jQuery.fn.jquery);
    </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> jQuery CDN tag के ऊपर रखना, इसलिए browser $ is not defined report करता है क्योंकि jQuery अभी तक load नहीं हुआ।
  2. CDN tag जोड़ना लेकिन यह कभी check न करना कि यह load हुआ, इसलिए एक blocked या गलत लिखा गया URL चुपचाप page पर हर $() call तोड़ देता है।
  3. jQuery को एक से ज़्यादा बार load करना (उदाहरण के लिए एक CDN tag plus एक local copy), जो पहली copy को overwrite कर सकता है और उससे जुड़े plugins मिटा सकता है।
🔒

Chapter Quiz — Complete all 6 topics to unlock

0/6 topics done

Complete these topics first:

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.