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:



  • url: It is required parameter. It holds the url to whom the request is send to.
  • success(response, status): It is optional parameter. It holds the function to run if the request got success.
    • response: It holds the result data from the request.
    • status: It holds the status of the request like- “success”, “notmodified”, “error”, “timeout”, or “parsererror”.

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

test.js
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.

filter_none

edit
close

play_arrow

link
brightness_4
code

<!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>                    

chevron_right


Output:

  • Before click on the button:
  • After click on the button:

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

filter_none

edit
close

play_arrow

link
brightness_4
code

<!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>                    

chevron_right


Output:

  • Before click on the button
  • After click on the button



My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.




Article Tags :

Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.