getScript() Method - Javascript jQuery Method and Property

Javascript examples for jQuery Method and Property:getScript

Description

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

Syntax

$.getScript(url,function(data,status))
ParameterRequire Description
url Required.url to send the request to
function(response,status) Optional. function to run if the request succeeds

Additional parameters:

  • response - result data from the request
  • status - status of the request ("success", "notmodified", "error", "timeout", or "parsererror")

The following code shows how to get and run a JavaScript using an AJAX request:

Demo Code

ResultView the demo in separate window

<!DOCTYPE html>
<html>
<head>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
<script>
$(document).ready(function(){
    $("button").click(function(){
        $.getScript("demo.js");
    });/*from w w w  .j ava  2  s  .  co  m*/
});
</script>
</head>
<body>

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

</body>
</html>

Related Tutorials