Normally jQuery statement $ (dollar) sign से start होता है और semicolon (;) से end होता है। जहां $ (dollar) sign, jQuery का alias है , means $ की जगह jQuery भी लिख सकते हैं।

jQuery Syntax

$(document).ready(function(){
   //write you logic here
});

or 

jQuery(document).ready(function(){
   //write you logic here
});

jQuery Example

Copy Fullscreen Close Fullscreen Run
<!DOCTYPE html>
<html>
  <head>
    <meta charset="utf-8">
    <title>jQuery Example</title>
  <script src="https://code.jquery.com/jquery-3.5.1.min.js"></script>
  </head>
  <body>
  <script type="text/javascript">
    $(document).ready(function()
    {
      alert("Hello World !");
    });
  </script>
  </body>
</html>

Explanation of the code

  • JavaScript की तरह ही jQuery code  भी <script> tag के अंदर ही place किया गया है।

  • document एक Document Object है जो कि window में show हो रहे web page को represent करता है।  इसे $() help select किया गया , jQuery में DOM Elements (such as <body>, <p>, <div>) को इसी तरह से select किया जाता है।

  • और ready एक event handler है जो कि callback function को as a parameter accept करता है और इस function के अंदर हम actual jQuery code लिखते हैं। यह simply बताता है कि all jQuery code document (web page) के पूरी तरह से load हो जाने के बाद ही run होगा।


$(document).ready(function(){}); की जगह shorthand $() use कर सकते हैं।

$(functiom(){
   // write you code here
});

चूंकि $(document).ready(function(){}); के अंदर लिखा code document (web page) के पूरी तरह से load हो जाने के बाद ही run होता है , but अगर आप web page load होते समय कोई code run करना चाहते हैं तो $(window).load(function(){}); event handler use कर सकते हैं।

See Example

File : jquery_syntax_test2.html

Copy Fullscreen Close Fullscreen Run
<!DOCTYPE html>
<html>
  <head>
    <meta charset="utf-8">
    <title>jQuery Another Example</title>
    <script src="jquery-3.5.1.js"></script>
  </head>
  <body>
  <script type="text/javascript">
    $(document).ready(function() {
      document.write("It will run after loading the page");
    });

    $(window).on('load', function(){
      document.write("It will run at the time of loading the page");
    });
  </script>
  </body>
</html>
Output
It will run at the time of loading the page
It will run after loading the page

example में window object , browser window को represent करता है , browser में हर एक tab को इसके खुद के window object से ही through represent किया जाता है।


जैसा कि आपने ऊपर पढ़ा कि $(document).ready(function(){}); एक callback function as a parameter accept करता है तो आप function define करके direct function का name भी pass कर सकते हैं।

File : jquery_syntax_test.html

Copy Fullscreen Close Fullscreen Run
<!DOCTYPE html>
<html>
  <head>
    <meta charset="utf-8">
    <title>jQuery Pass Function As Callback Functoin</title>
    <script src="jquery-3.5.1.js"></script>
  </head>
  <body>
    <script type="text/javascript">
      /*define the function*/
      function myfun(){
        document.write("Inside Function");
      }
      $(document).ready(myfun);
      /*we can also do this*/
      $(myfun);
    </script>
  </body>
</html>
Output
Inside Function
Inside Function

Related Topics :

Rahul Kumar

Rahul Kumar

Hi ! My name is Rahul Kumar Rajput. I'm a back end web developer and founder of learnhindituts.com. I live in Uttar Pradesh (UP), India and I love to talk about programming as well as writing technical tutorials and tips that can help to others.

Get connected with me. :) LinkedIn Twitter Instagram Facebook

b2eprogrammers