jQuery getScript() Method

jQuery getScript() method used to loads JavaScript data from the server using a HTTP GET request.

Syntax

Here is a syntax for the getScript() method

$.getScript( url, function(response, textStatus) );
Parameter Type Description
URL String Required. Specifies the URL to send the request to.
success(response, textStatus) Function Optional. A callback function to be run when the request succeeds.
Additional parameters
  • response - contains the result data from the request
  • textStatus - contains the status of the request

Example

getscript.js
function nameAlert(){
   alert("My name is Paul.");
}

Here, in this example getScript() method call when click on button.

<!DOCTYPE html>
<html>
<head>
  <title>jQuery get() method</title>
  <script src="jquery-latest.min.js"></script>
  <script>
    $(document).ready(function(){
      $("button").click(function(){
        $.getScript("getscript.js",function(){
          nameAlert();
        });
      });
    });
  </script>
</head>
<body>
  <div id="msg">This text replace with new contains</div>
  <br />
  <button>Click here to call getScript() method</button>
</body>
</html>

Run it...   »