Sign Up for Free

RunKit +

Try any Node.js package right in your browser

This is a playground to test code. It runs a full Node.js environment and already has all of npm’s 400,000 packages pre-installed, including web-storages with all npm packages installed. Try it out:

var webStorages = require("web-storages")

This service is provided by RunKit and is not affiliated with npm, Inc or the package authors.

web-storages v0.1.3

An emulator for localStorage and sessionStorage

web-storages

Version Build status

Emulating localStorage and sessionStorage if not exist.

Install

Install with npm:

npm install web-storages

API - Set functions by args

    // On node.js, storages will attached to global automatically.
    // Take care for that exporting storages are not equals global storages.
    require('web-storages');
    
    // localStorage
    localStorage.setItem('hoge', 'Hello,hoge!');
    localStorage.setItem('fuga', 'Hello,fuga!');
    console.log(localStorage.getItem('hoge')); // => 'Hello,hoge!'
    console.log(localStorage.key(0)); // => 'hoge'
    console.log(localStorage.length()); // => 2
    
    localStorage.removeItem('hoge');
    console.log(localStorage.getItem('hoge'))); // => null
    console.log(localStorage.length()); // => 1
    
    localStorage.clear();
    console.log(localStorage.getItem('fuga'))); // => null
    console.log(localStorage.length()); // => 0
    
    // sessionStorage
    sessionStorage.setItem('hoge', 'Hello,hoge!');
    sessionStorage.setItem('fuga', 'Hello,fuga!');
    console.log(sessionStorage.getItem('hoge')); // => 'Hello,hoge!'
    console.log(sessionStorage.key(0)); // => 'hoge'
    console.log(sessionStorage.length()); // => 2
    
    sessionStorage.removeItem('hoge');
    console.log(sessionStorage.getItem('hoge'))); // => null
    console.log(sessionStorage.length()); // => 1
    
    sessionStorage.clear();
    console.log(sessionStorage.getItem('fuga'))); // => null
    console.log(sessionStorage.length()); // => 0

also use on browser

On browser, storages will attached to window automatically.
Now, almost all browsers have implemented web-storage, so that
native module is used in natural.

If you want to use this storages on a modern browser,
you have to prepare "module.exports" object or "global" object
on window namespace.

Ways to import module with using "require-js"
or "foonyah" is awesome way for importing module.

And more, 4 functions to access "document.cookie" are attached to exports.
docCookie(document) ... change cookie document to arguments[0] provided.
keyCookie() ... get cookies key array.
setCookie(key, value, options) ... set a cookie value. options accepts or to given days of period.
getCookie(key) .. get a cookie value.

<script type="text/javascript">

    var src = 'https://raw.github.com/ystskm/web-storages-js/master/web-storages.js';
    foonyah.require(src).done(function(){
      localStorage.setItem( ... )
    });

</script>
RunKit is a free, in-browser JavaScript dev environment for prototyping Node.js code, with every npm package installed. Sign up to share your code.
Sign Up for Free