jQuery getScript()

Introduction

Get and run a JavaScript using an AJAX request:

View in separate window

<!DOCTYPE html>
<html>
<head>
<script 
 src="https://ajax.googleapis.com/ajax/libs/jquery/3.4.1/jquery.min.js">
</script>/*ww w  .  j a v  a  2  s.c  om*/
<script>
$(document).ready(function(){
  $("button").click(function(){
       $.getScript("ajax.js");
  });
});
</script>
</head>
<body>

<button>Use Ajax to get and then run a JavaScript</button>

</body>
</html>

The getScript() method gets and executes a JavaScript using an AJAX HTTP GET request.

$(selector).getScript(url,success(response,status))
Parameter
Optional
Description
url
Required.
the url to send the request to
success(response,status)








Optional.








the function to run if the request succeeds
Additional parameters:
response - the result data from the request
status - the status of the request
"success"
"notmodified"
"error"
"timeout"
"parsererror"



PreviousNext

Related