jQuery.getScript()
Categories: Shorthand Methods
jQuery.getScript( url, [ success(script, textStatus, jqXHR) ] )Returns: jqXHR
Description: Load a JavaScript file from the server using a GET HTTP request, then execute it.
-
jQuery.getScript( url, [ success(script, textStatus, jqXHR) ] )
version added: 1.0url A string containing the URL to which the request is sent.
success(script, textStatus, jqXHR) A callback function that is executed if the request succeeds.
This is a shorthand Ajax function, which is equivalent to:
$.ajax({ url: url, dataType: "script", success: success });
The script is executed in the global context, so it can refer to other variables and use jQuery functions. Included scripts can have some impact on the current page.
Success Callback
The callback is passed the returned JavaScript file. This is generally not useful as the script will already have run at this point.
$(".result").html("<p>Lorem ipsum dolor sit amet.</p>");
Scripts are included and run by referencing the file name:
$.getScript("ajax/test.js", function(data, textStatus, jqxhr) { console.log(data); //data returned console.log(textStatus); //success console.log(jqxhr.status); //200 console.log('Load was performed.'); });
Handling Errors
As of jQuery 1.5, you may use .fail()
to account for errors:
$.getScript("ajax/test.js") .done(function(script, textStatus) { console.log( textStatus ); }) .fail(function(jqxhr, settings, exception) { $( "div.log" ).text( "Triggered ajaxError handler." ); });
Prior to jQuery 1.5, the global .ajaxError()
callback event had to be used in order to handle $.getScript()
errors:
$( "div.log" ).ajaxError(function(e, jqxhr, settings, exception) { if (settings.dataType=='script') { $(this).text( "Triggered ajaxError handler." ); } });
Caching Responses
Be default, $.getScript()
sets the cache setting to false
. This appends a timestamped query parameter to the request URL to ensure that the browser downloads the script each time it is requested. You can override this feature by setting the cache property globally using $.ajaxSetup()
:
$.ajaxSetup({ cache: true });
Alternatively, you could define a new method that uses the more flexible $.ajax()
method.
-
Define a $.cachedScript() method that allows fetching a cached script:
Code:
jQuery.cachedScript = function(url, options) { // allow user to set any option except for dataType, cache, and url options = $.extend(options || {}, { dataType: "script", cache: true, url: url }); // Use $.ajax() since it is more flexible than $.getScript // Return the jqXHR object so we can chain callbacks return jQuery.ajax(options); }; // Usage $.cachedScript("ajax/test.js").done(function(script, textStatus) { console.log( textStatus ); });
-
Load the official jQuery Color Animation plugin dynamically and bind some color animations to occur once the new functionality is loaded.
HTML:
<button id="go">» Run</button> <div class="block"></div>
CSS:
.block { background-color: blue; width: 150px; height: 70px; margin: 10px; }
Code:
$.getScript("/scripts/jquery.color.js", function() { $("#go").click(function(){ $(".block").animate( { backgroundColor: "pink" }, 1000) .delay(500) .animate( { backgroundColor: "blue" }, 1000); }); });