← Back to jQuery Course | Chapter 14: Custom Plugins | Lesson 1 of 4

मौजूदा Plugins Integrate करना

एक jQuery plugin reusable code है जो jQuery में एक feature add करता है। Plugins effects, widgets, validation, और दूसरी features add कर सकते हैं।
Syntax
javascript
<script src="jquery.js"></script>
<script src="plugin-name.js"></script>   <!-- after jQuery -->

$("selector").pluginName({option: value});

एक jQuery Plugin क्या है?

एक jQuery plugin jQuery.fn से attached एक function है ताकि यह किसी भी jQuery selection पर callable हो जाए, library की built-in क्षमता से आगे extend करते हुए।

उदाहरण: What is a jQuery Plugin?

javascript
<!DOCTYPE html>
<html>
  <head>
    <script src="https://code.jquery.com/jquery-3.7.1.min.js"></script>
  </head>
  <body>
    <p>Text</p>
    <script>
      $.fn.highlight = function() {
      return this.css("background", "yellow");
      };
      $("p").highlight();
    </script>
  </body>
</html>

एक Plugin Include करना

एक plugin आमतौर पर jQuery खुद के बाद रखे गए एक script tag से load किया जाता है, क्योंकि इसे नए methods attach करने से पहले jQuery.fn का पहले से exist करना ज़रूरी है।

उदाहरण: Include a Plugin

javascript
<!DOCTYPE html>
<html>
  <head>
    <script src="https://code.jquery.com/jquery-3.7.1.min.js"></script>
  </head>
  <body>
    <script>
      // Plugin script tags must come after the jQuery script tag,
      // since they attach methods onto jQuery.fn which must already exist
      console.log(typeof $.fn.on);
    </script>
  </body>
</html>

Plugin Options इस्तेमाल करना

अच्छे plugins अक्सर एक argument के रूप में options object accept करते हैं, users को behavior customize करने देते हुए — colors, speeds, callback functions — बिना।

उदाहरण: Use Plugin Options

javascript
<!DOCTYPE html>
<html>
  <head>
    <script src="https://code.jquery.com/jquery-3.7.1.min.js"></script>
  </head>
  <body>
    <p>Text</p>
    <script>
      $.fn.highlight = function(options) {
      const settings = $.extend({color: "yellow"}, options);
      return this.css("background", settings.color);
      };
      $("p").highlight({color: "pink"});
    </script>
  </body>
</html>

एक Plugin Initialize करना

कई plugins एक selected element पर एक method call करके initialize होते हैं, जैसे $('.carousel').slick(), और initialization आमतौर पर एक document-ready के अंदर किया जाता है।

उदाहरण: Initialize a Plugin

javascript
<!DOCTYPE html>
<html>
  <head>
    <script src="https://code.jquery.com/jquery-3.7.1.min.js"></script>
  </head>
  <body>
    <div class="carousel">Slide</div>
    <script>
      $.fn.highlight = function() { return this.css("background", "yellow"); };
      $(document).ready(function() {
      $(".carousel").highlight();
      });
    </script>
  </body>
</html>

एक छोटा Plugin Integration Project

किसी project में third-party plugin add करने से पहले, इसकी documentation में required files, सही initialization method, available।

उदाहरण: Small Plugin Integration Project

javascript
<!DOCTYPE html>
<html>
  <head>
    <script src="https://code.jquery.com/jquery-3.7.1.min.js"></script>
  </head>
  <body>
    <script>
      // Before adding a plugin, check: required files, init method, options, browser support
      console.log("Checklist before integrating a third-party plugin");
    </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. jQuery से पहले plugin script include करना, ताकि jQuery.fn undefined हो और plugin load होने में fail हो जाए।
  2. एक ऐसा plugin method call करना जो exist नहीं करता, जैसे $("p").highlightX(), एक typo या missing script की वजह से।
  3. गलत form में options pass करना, जैसे object के बजाय एक string, ताकि plugin उन्हें ignore करे और अपने defaults इस्तेमाल करे।
🔒

Chapter Quiz — Complete all 4 topics to unlock

0/4 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.