A tiny, Promise-based vanilla JS Ajax/HTTP plugin with great browser support.
Vanilla JS Pocket Guides or join the Vanilla JS Academy and level-up as a web developer. 🚀Want to learn how to write your own vanilla JS plugins? Check out my
Compiled and production-ready code can be found in the
dist directory. The
src directory contains development code.
1. Include Atomic on your site.
There are two versions of Atomic: the standalone version, and one that comes preloaded with a polyfill for ES6 Promises, which are only supported in newer browsers.
If you're including your own polyfill or don't want to enable this feature for older browsers, use the standalone version. Otherwise, use the version with the polyfill.
You can also use the jsDelivr CDN. I recommend linking to a specific version number or version range to prevent major updates from breaking your site. Smooth Scroll uses semantic versioning.
<!-- Always get the latest version --><!-- Not recommended for production sites! --><!-- Get minor updates and patch fixes within a major version --><!-- Get patch fixes within a minor version --><!-- Get a specific version -->
You can also use NPM (or your favorite package manager).
npm install atomicjs
2. Make your Ajax request.
Pass in the requested URL, and optionally, your desired options. Request method defaults to
.then() with a callback to handle successful responses, and
catch() to handle errors.
// A basic GET request;// A POST request;
Atomic does not have a default export, but does support CommonJS and can be used with native ES6 module imports.
It uses a UMD pattern, and should also work in most major module bundlers and package managers.
Working with the Source Files
If you would prefer, you can work with the development code in the
src directory using the included Gulp build system. This compiles, lints, and minifies code.
Make sure these are installed first.
- In bash/terminal/command line,
cdinto your project directory.
npm installto install required files.
- When it's done installing, run one of the task runners to get going:
gulpmanually compiles files.
gulp watchautomatically compiles files when changes are made and applies changes using LiveReload.
gulp testruns unit tests.
Options and Settings
Atomic includes smart defaults and works right out of the box. You can pass options into Atomic through the
Canceling a Request
While Promises can't be canceled, Atomic does have an internal API for aborting your XHR request using the
In order to work, you must set your
atomic() method to a variable without
.then() methods. They can be called on the variable after setting.
// Setup your requestvar xhr = ;// Handle responsesxhr;// Cancel your requestxhr;
Migrating from Atomic 3
- Atomic is now Promise-based, and supports chaining with
- For simple requests, you can now just pass in a URL to
atomic()like you would with the Fetch API. You no longer need to pass in an object with the
- You must pass in a URL as the first argument. The URL as an options parameter is no longer support.
- You now pass arguments directly into
atomic.ajax()method no longer exists.
.always()callbacks have been removed. Use
- JSONP support has been removed.
You can still download Atomic 3 and earlier on the releases page.
Atomic works in all modern browsers, and IE8 and above.
The standalone version provides native support for all modern browsers. Use the
.polyfills version (or include your own) to support IE.
The code is available under the MIT License.