jQuery.getScript(url [, success]) (since 1.0) is a GET shorthand that loads a JavaScript file and executes it in the global context. It is equivalent to $.ajax({ url, dataType: "script", success }).
Syntax
Start with these common calls. Keep them short and readable.
Learn jQuery.getScript(): the script GET shorthand for $.ajax(), load and execute JavaScript in the global context, success after run, cache:false by default, $.cachedScript() pattern, and .done()/.fail() error handling.
Load jQuery before your script (CDN or local file), then wrap DOM work in $(function () { ... }) when needed.
Parameters
Parameter
Description
Step 1
Call $.getScript(url) with an optional success callback.
Step 2
jQuery GETs the file with dataType: "script" (cache off by default).
Step 3
The script runs globally — plugins attach to window.
Return Value
Return Value
Description
jQuery object
Most setters and traversing helpers return the jQuery collection so you can chain more methods.
jQuery Examples
Example 1 — Basic $.getScript()
Load dayjs from a CDN and format today’s date after it executes.
Valid URL → .done() shows Loaded (HTTP 200)
Bad URL → .fail() shows error + status
Use jQuery 3.5+ for safer failed-script behavior
How It Works
Return jqXHR.$.getScript() returns a Promise-like object.
Success..done() runs after load + execute.
Failure. 404 / network errors go to .fail().
Frequently Asked Questions
jQuery.getScript() is a shorthand for $.ajax() with dataType "script". Pass a URL and optional success callback — jQuery fetches the file, executes it in the global context, then runs your callback. It returns a jqXHR so you can chain .done(), .fail(), and .always().
After the script has been loaded and executed. That is when globals defined by the file (plugins, utilities) are safe to use.
$.getScript() sets cache to false, which appends a timestamp query parameter so the browser downloads a fresh copy each time. For stable CDN libraries, use the official $.cachedScript() wrapper (cache: true) or $.ajax({ dataType: "script", cache: true }).
Since jQuery 1.5, chain .fail() on the returned jqXHR. Before 1.5, use a global ajaxError handler and check settings.dataType === "script". Prefer jQuery 3.5+ — older versions could still execute unsuccessful HTTP responses that had a script Content-Type.
Both load and run JavaScript. $.getScript() goes through jQuery.ajax(), so you get jqXHR Promises, global Ajax events, and consistent .fail() handling. A raw script element gives more control over attributes but no built-in Promise unless you wrap it.
No. Scripts run with full page privileges. Only load from trusted origins, never pass user-controlled URLs, and prefer allowlists plus Content Security Policy.