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 }).
Check these before loading remote scripts in production.
security
Scripts run with full page power
Only load from trusted origins. Never pass user-controlled URLs to $.getScript().
timing
Use APIs only after success
Code before the callback cannot assume the library exists. Bind UI in .done() / success.
cache
Cache is off by default
cache: false adds a timestamp. For stable CDN files, use $.cachedScript() or $.ajax({ cache: true }).
version
Prefer jQuery 3.5+
Older versions could execute unsuccessful HTTP responses that still had a script Content-Type.
modules
Not an ES module loader
$.getScript() runs classic scripts globally. Use native type="module" / import() for ES modules.
Compatibility
Browser Support
jQuery.getScript() has been part of jQuery since 1.0. Promise .fail() chaining needs 1.5+. Prefer jQuery 3.5+ for safer handling of failed script HTTP responses.
✓ Stable · jQuery 1.0+
jQuery.getScript()
Works in every browser that runs your loaded jQuery version. Prefer a current jQuery 3.x (3.5+) build. Requires jQuery Ajax — native dynamic script tags and ES modules are separate APIs.
100%With jQuery Ajax
Google ChromeAll versions · Desktop & Mobile
Full support
Mozilla FirefoxAll versions · Desktop & Mobile
Full support
Apple SafariAll versions · macOS & iOS
Full support
Microsoft EdgeAll versions · Chromium & Legacy
Full support
Internet ExplorerIE 6+ · Legacy environments
Full support
OperaAll modern versions
Full support
getScriptUniversal
Bottom line: Safe for trusted CDN / same-origin scripts. Always chain .fail() and keep URLs allowlisted.
Analysis
Performance Notes
Approach
Cost
Best for
One lazy $.getScript()
Cheap
On-demand plugins / widgets
cachedScript for CDN libs
Cheaper repeats
Stable third-party files
Many uncached getScript calls
Expensive
Avoid — cache or bundle instead
Load scripts only when needed. Busting cache on every call is fine in development; enable caching for production CDN libraries.
Remember
Key Takeaways
Role: GET + execute JavaScript globally (dataType: "script").
Call:$.getScript(url [, success]) — use APIs in the callback.
Cache: off by default; use cachedScript / cache: true for CDN libs.
One line:$.getScript() loads and runs a remote script — call its APIs only after success, and never from untrusted URLs.
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.