A RetroSearch Logo

Home - News ( United States | United Kingdom | Italy | Germany ) - Football scores

Search Query:

Showing content from http://api.jquery.com/jQuery.getScript/ below:

jQuery.getScript() | jQuery API Documentation

jQuery.getScript( url [, success ] )Returns: jqXHR

Description: Load a JavaScript file from the server using a GET HTTP request, then execute it.

This is a shorthand Ajax function, which is equivalent to:

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 fired once the script has been loaded and executed.

Scripts are included and run by referencing the file name:

1

2

3

4

5

6

$.getScript( "ajax/test.js", function( data, textStatus, jqxhr ) {

console.log( textStatus );

console.log( jqxhr.status );

console.log( "Load was performed." );

Handling Errors

As of jQuery 1.5, you may use .fail() to account for errors:

1

2

3

4

5

6

7

$.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:

1

2

3

4

5

$( "div.log" ).on( "ajaxError", function( e, jqxhr, settings, exception ) {

if ( settings.dataType == "script" ) {

$( this ).text( "Triggered ajaxError handler." );

Prior to jQuery 3.5.0, unsuccessful HTTP responses with a script Content-Type were still executed.

Caching Responses

By 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():

Alternatively, you could define a new method that uses the more flexible $.ajax() method.

Examples: Example 1

Define a $.cachedScript() method that allows fetching a cached script:

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

jQuery.cachedScript = function( url, options ) {

options = $.extend( options || {}, {

return jQuery.ajax( options );

$.cachedScript( "ajax/test.js" ).done(function( script, textStatus ) {

console.log( textStatus );

Example 2

Load the official jQuery Color Animation plugin dynamically and bind some color animations to occur once the new functionality is loaded.

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

<title>jQuery.getScript demo</title>

<script src="https://code.jquery.com/jquery-3.7.1.js"></script>

<button id="go">&raquo; Run</button>

<div class="block"></div>

var url = "https://code.jquery.com/color/jquery.color-2.1.2.js";

$.getScript( url, function() {

$( "#go" ).on( "click", function() {

backgroundColor: "rgb(255, 180, 180)"

Demo:

RetroSearch is an open source project built by @garambo | Open a GitHub Issue

Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo

HTML: 3.2 | Encoding: UTF-8 | Version: 0.7.4