storage-available
v1.1.0
Published
Detect if web storage is available and functional
Downloads
4,693
Maintainers
Readme
storage-available
Detect if web storage is available and functional
.
This microscopically small module contains only a single function, storageAvailable
, that detects whether the Web Storage API methods are available. The reason you would want to use a separate module for feature detecting Web Storage is because it is hard and error prone.
This function is the one recommended by the MDN documentation on Web Storage. I can claim copyright (and share it with you under the MIT license) because I contributed that part of the docs.
Install
npm install --save storage-available
Require
var storageAvailable = require('storage-available')
Import
import storageAvailable from 'storage-available'
Usage
if (storageAvailable('localStorage')) {
// Yippee! We can use localStorage awesomeness
}
else {
// Too bad, no localStorage for us
}
You can test for sessionStorage instead by calling storageAvailable('sessionStorage')
.
NOTE: This module is intended for use in the browser, or other environments that natively support Web Storage. Use it via a web bundler such as Browserify or Webpack.
Credits
Credits to Paul Irish for hist gist summarizing the development of feature detecting localStorage, and to all people who contributed fixes to it in that process.
Credits to Lucius Gaitán for his PR which adds Typescript support!
Issues
Add an issue in this project's issue tracker to let me know of any problems you find, or questions you may have.
Copyright
Copyright 2016, 2018 by Stijn de Witt and Lucius Gaitán. Some rights reserved.
License
Licensed under the MIT Open Source license.