Skip to Main Content
jQuery Pocket Reference
book

jQuery Pocket Reference

by David Flanagan
December 2010
Intermediate to advanced content levelIntermediate to advanced
160 pages
3h 31m
English
O'Reilly Media, Inc.
Content preview from jQuery Pocket Reference

Ajax Utility Functions

The other high-level jQuery Ajax utilities are functions, not methods, and they are invoked directly through jQuery or $, not on a jQuery object. jQuery.getScript() loads and executes files of JavaScript code. jQuery.getJSON() loads a URL, parses it as JSON, and passes the resulting object to the specified callback. Both of these functions call jQuery.get(), which is a more general purpose URL-fetching function. Finally, jQuery.post() works just like jQuery.get() but performs an HTTP POST request instead of a GET. Like the load() method, all of these functions are asynchronous: they return to their caller before anything is loaded, and they notify you of the results by invoking a callback function that you specify.

jQuery.getScript()

The jQuery.getScript() function takes the URL of a file of JavaScript code as its first argument. It asynchronously loads and then executes that code in the global scope. It can work for both same-origin and cross-origin scripts:

// Dynamically load a script from some other server
jQuery.getScript("http://example.com/js/widget.js");

You can pass a callback function as the second argument, and if you do, jQuery will invoke that function once after the code has been loaded and executed:

// Load a library and use it once it loads
jQuery.getScript("js/jquery.my_plugin.js", function() {
    $('div').my_plugin();  // Use the library we loaded
});

jQuery.getScript() normally uses an XMLHttpRequest object to fetch the text of the script to be ...

Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Start your free trial

You might also like

jQuery and JavaScript Phrasebook

jQuery and JavaScript Phrasebook

Brad Dayley
Beyond jQuery

Beyond jQuery

Ray Nicholus
Head First jQuery

Head First jQuery

Ryan Benedetti, Ronan Cranley

Publisher Resources

ISBN: 9781449398958Supplemental ContentErrata Page