Home >>jQuery Tutorial >jQuery $.getScript() Method

jQuery $.getScript() Method

jQuery $.getScript() Method

jQuery $.getScript() method in jQuery is used to get and execute a JavaScript by using an AJAX HTTP GET request.

Syntax:

$(selector).getScript(url,success(response,status))

Parameter Values

Parameter Description
url It is s Required parameter and is used to specifies the url to send the request to
success(response,status) It is an Optional parameter and is used to specifies the function to run if the request succeeds
Additional parameters:
  • response – It is used to contains the result data from the request
  • status – It is used to contains the status of the request ("success", "notmodified", "error", "timeout", or "parsererror")

Here is an Example of jQuery getScript() Method:


<html>
<head>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.4.1/jquery.min.js"></script>
<script>
$(document).ready(function(){
$(".btn1").click(function(){
$.getScript("demo_ajax_script.js");
});
});
</script>
</head>
<body>
<button class="btn1">Click me to Use Ajax </button>
</body>
</html>

Output:

No Sidebar ads