Load the content of the file "ajax.txt" into a specific <div> element:
<!DOCTYPE html> <html> <head> <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.4.1/jquery.min.js"> </script>/*from w ww. ja va2s. c o m*/ <script> $(document).ready(function(){ $("button").click(function(){ $("#div1").load("ajax.txt"); }); }); </script> </head> <body> <div id="div1"><h2>Here</h2></div> <button>Get External Content</button> </body> </html>
The load()
method loads data from a server and puts returned data into the selected element.
$(selector).load(url,data,function(response,status,xhr))
Parameter | Optional | Description |
---|---|---|
url | Required. | the URL you wish to load |
data | Optional. | data to send to the server along with the request |
function(response,status,xhr) | Optional. | a callback function to run when the load() method is completed.Additional parameters: response - the result data from the request status - the status of the request "success" "notmodified" "error" "timeout" "parsererror" xhr - contains the XMLHttpRequest object |