If tutorials available on this website are helpful for you, please whitelist this website in your ad blocker😭 or Donate to help us ❤️ pay for the web hosting to keep the website running.
Normally jQuery statement $ (dollar) sign से start होता है और semicolon (;) से end होता है। जहां $ (dollar) sign, jQuery का alias है , means $ की जगह jQuery भी लिख सकते हैं।
$(document).ready(function(){ //write you logic here }); or jQuery(document).ready(function(){ //write you logic here });
<!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>
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
<!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>
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
<!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>
Hi ! I'm Rahul Kumar Rajput founder of learnhindituts.com. I'm a software developer having more than 4 years of experience. I love to talk about programming as well as writing technical tutorials and blogs that can help to others. I'm here to help you navigate the coding cosmos and turn your ideas into reality, keep coding, keep learning :)
Get connected with me. :) LinkedIn Twitter Instagram Facebook