jQuery jQuery.getScript()
Learn all about the jQuery function jQuery.getScript().
This is a shorthand Ajax function, which is equivalent to:
1
2
3
4
5
|
|
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 but not necessarily executed.
Scripts are included and run by referencing the file name:
1
2
3
4
5
6
|
|
Handling Errors
As of jQuery 1.5, you may use .fail()
to account for errors:
1
2
3
4
5
6
7
|
|
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
|
|
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()
:
1
2
3
|
|
Alternatively, you could define a new method that uses the more flexible $.ajax()
method.