It's quite simple!
Standard prozedure, include jquery
and jquery.storage.js
:
<script src="https://github.com/yckart/jquery.storage.js/raw/master/jquery.js"></script>
<script src="https://github.com/yckart/jquery.storage.js/raw/master/jquery.storage.js"></script>
<!--
You can optional include a $.cookie plugin
to support older browsers (e.q. IE lt 7)
I recommend the plugin from Klaus Hartl:
https://github.com/carhartl/jquery-cookie
(but you can of course use any other plugin with an equal syntax)
<script src="https://github.com/yckart/jquery.storage.js/raw/master/jquery.cookie.js"></script>
-->
$.localStorage('foo', 'bar');
$.sessionStorage('foo', 'bar');
To get a single value just pass your key as a string
:
$.localStorage('foo'); // 'bar'
for multiple returns use an array instead:
$.localStorage(['key1', 'key2']); // ['value1', 'value2']
To get a single value just pass your key as a string
:
$.sessionStorage('foo'); // 'bar'
for multiple returns use an array instead:
$.sessionStorage(['key1', 'key2']); // ['value1', 'value2']
$.localStorage('foo', null);
$.sessionStorage('foo', null);
option | description | default |
---|---|---|
cookiePrefix |
Defines a prefix to prepend to your cookie-fallback | 'fallback:' + method + ':' |
path |
The cookie path to use. | '/' |
domain |
The cookie domain to use. | document.domain |
expires |
The time when your cookie has to expire. | ('localStorage' === method) ? { expires: 365 } : undefined |
$.localStorage.options = {
cookiePrefix : 'fallback:' + method + ':',
cookieOptions : {
path : '/',
domain : document.domain,
expires : ('localStorage' === method) ? { expires: 365 } : undefined
}
};
There're some public methods which you can use instead of the default syntax.
...by the way the following methods can (of course) also used with $.sessionStorage()
:
var ls = $.localStorage();
var ss = $.sessionStorage();
setItem()
To set a storage-item you can attach setItem
to your storage
-instance:
ls.setItem('foo', 'bar');
getItem()
gets an earlier setted, it is just as easy as setItem
.
Attach getItem
to your instance and pass the key/s (as a string
) to it:
ls.getItem('foo'); // 'bar'
If you need to get more than one value
pass your keys
as an array
to getItem
:
ls.getItem(['key1', 'key2']); // ['value1', 'value2']
removeItem()
removes an earlier defined item:
ls.removeItem('foo') // null
clear()
Sometimes you need to remove all key-value pairs, dont worry this is just as easy as anything else:
ls.clear();
Storage gives you two new jQuery.support
-properties:
$.support.localStorage
| Tests for localStorage
-support$.support.sessionStorage
| Tests for sessionStorage
-supportBoth return true
or false
, depending on the availability.
Get the raw script, download the complete package or fork it on GitHub.
@yckart #jquery #storage
Make it possible to pass the options as object to the $.storage
-method, like this:
var ls = $.localStorage({
cookiePrefix : 'fallback:' + method + ':',
cookieOptions : {
path : '/',
domain : document.domain,
expires : ('localStorage' === method) ? { expires: 365 } : undefined
}
});
Copyright (c) 2013 Yannick Albert (http://yckart.com/)
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.