$.load() method jQuery का सबसे simple method है AJAX Request Send करने के लिए। load() method दिए गए URL से data load करके selected element में put करता है।

jQuery $.load Syntax

$(".target_elm").load('url', data, callback_function)

url : वो URL जिसके लिए हम request send करना चाहते हैं , यहाँ हम किसी file का name भी दे सकते हैं file content load करने के लिए।

data : यह Optional होता है , जिसमे हम Query String ('key = value' pair) form में data send करते हैं।

callback function : function जो request end होने के बाद run होता है , जो की 3 parameter (response , status , xhr) होते हैं। यह Optional होता है .

jQuery $.load() Example

File : jquery_load.html

CopyFullscreenClose Fullscreen
<!DOCTYPE html>
<html>
  <head>
    <meta charset="utf-8">
    <title>jQuery $.load() Method </title>
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.5.1/jquery.min.js"></script>
  </head>	
  <body>
  	<p class="target_elm">Click bellow buttons button to see result.</p>
    <p>
      <button class="send_request">load() Content</button>
    </p>
    <script type="text/javascript">
      window.onload = function()
      {
        /*check if jquery working*/
        if(window.jQuery)
        {
          $(".send_request").click(function(event) {
            $(".target_elm").load('handle_request.php', 'name=Rahul', function(response, status, xhr){
              /*see in console , what's coming*/
              console.log('Response ', response);
              console.log('status', status);
              console.log('xhr ', xhr);
            });
          }); 
        }
      }
  	</script>
  </body>
</html>

File : handle_request.php

Copy Fullscreen Close Fullscreen
<?php
	echo 'Request Recieved';
	echo '<br>'; /*for line break*/
	echo 'Name : '. $_REQUEST['name'];
?>

अब अगर $.load () method के through किसी file का content load करना हो तो कुछ इस तरह से करते हैं।

$('#target_div').load('file.txt');

Related Topics :

Rahul Kumar

Rahul Kumar

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

b2eprogrammers