English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية

jQuery getScript() Method

jQuery AJAX Methods

The $.getScript() method uses a GET HTTP request to load a JavaScript file from the server and then execute the file.

Syntax:

$.getScript(URL, callback)

Example

Use AJAX requests to request and execute JavaScript:

$("button").click(function(){
  $.getScript("/javascript/myscript.js");
});
Test to see‹/›

ThecallbackThe script is triggered once, but it may not necessarily be executed::

$("button").click(function(){
  $.getScript("/javascript/myscript.js, function(data, status, xhr){
    alert(data);   // Data returned
    alert(status); // Success
    alert(xhr.status); // 200
  });
});
Test to see‹/›

Parameter Value

ParametersDescription
URLSpecify the URL you want to request
callback(Optional) Specify the callback function to be executed after the request is successful

Parameters:

  • data-Contains the result data from the request

  • status -Contains the request status ("success", "notmodified", "error", "timeout", "parsererror")

  • xhr-Contains XMLHttpRequest object

jQuery AJAX Methods