Open In App

jQuery getScript() Method

The getScript() method in jQuery is used to run a JavaScript using AJAX HTTP GET request.

Syntax:



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

Parameters: It contains two parameters as mentioned above and described below:

The test.js file stored on server and it will load after clicking the change content button.



alert("Get javaScript using AJAX HTTP GET request");

Example 1: This example display an alert message from the JavaScript received by AJAX HTTP GET request.




<!DOCTYPE html>
<html>
 
<head>
    <script src=
    </script>
 
    <script>
        $(document).ready(function () {
            $("button").click(function () {
                $.getScript("test.js");
            });
        });
    </script>
</head>
 
<body style="text-align:center;">
 
    <h1 style="color: green;">
        GeeksforGeeks
    </h1>
 
    <p id="paragraph" style="font-size: 20px;">
        A computer science portal for geeks
    </p>
 
    <button>
        Get JavaScript using an AJAX HTTP GET request.
    </button>
</body>
 
</html>

Output:

Example 2: This example use getScript() Method to display an alert message.




<!DOCTYPE html>
<html>
 
<head>
    <script src=
    </script>
 
    <!-- Script to use getScript() Method -->
    <script>
        $(document).ready(function () {
            $("button").click(function () {
                $.getScript("test.js",
                    alert("JavaScript Received "));
            });
        });
    </script>
</head>
 
<body style="text-align:center;">
 
    <h1 style="color: green;">
      GeeksforGeeks
    </h1>
 
    <p id="paragraph" style="font-size: 20px;">
        A computer science portal for geeks
    </p>
 
    <button>
        Get JavaScript using an AJAX HTTP GET request.
    </button>
</body>
 
</html>

Output:


Article Tags :