2 * jQuery Plugin to use Local Storage or Session Storage without worrying
3 * about HTML5 support. It uses Cookies for backward compatibility.
5 * @author Alberto Varela Sánchez (http://www.berriart.com)
6 * @version 1.0 (17th January 2013)
8 * Released under the MIT License (http://opensource.org/licenses/MIT)
10 * Copyright (c) 2013 Alberto Varela Sánchez (alberto@berriart.com)
12 * Permission is hereby granted, free of charge, to any person obtaining a
13 * copy of this software and associated documentation files (the "Software"),
14 * to deal in the Software without restriction, including without limitation
15 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
16 * and/or sell copies of the Software, and to permit persons to whom the
17 * Software is furnished to do so, subject to the following conditions:
19 * The above copyright notice and this permission notice shall be included in
20 * all copies or substantial portions of the Software.
22 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
23 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
24 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
25 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
26 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
27 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
31 ;(function(window, $ ) {
34 var types = ['localStorage','sessionStorage'],
37 $.each(types, function( i, type ) {
39 support[type] = type in window && window[type] !== null;
41 support[type] = false;
46 cookiePrefix : 'html5fallback:' + type + ':',
49 domain : document.domain,
50 expires : ('localStorage' === type) ? { expires: 365 } : undefined
54 getItem : function( key ) {
57 response = window[type].getItem(key);
60 response = $.cookie(this.settings.cookiePrefix + key);
66 setItem : function( key, value ) {
68 return window[type].setItem(key, value);
71 return $.cookie(this.settings.cookiePrefix + key, value, this.settings.cookieOptions);
75 removeItem : function( key ) {
77 return window[type].removeItem(key);
80 var options = $.extend(this.settings.cookieOptions, {
83 return $.cookie(this.settings.cookiePrefix + key, null, options);
89 return window[type].clear();
92 var reg = new RegExp('^' + this.settings.cookiePrefix, ''),
93 options = $.extend(this.settings.cookieOptions, {
97 if(document.cookie && document.cookie !== ''){
98 $.each(document.cookie.split(';'), function( i, cookie ){
99 if(reg.test(cookie = $.trim(cookie))) {
100 $.cookie( cookie.substr(0,cookie.indexOf('=')), null, options);