| 8842 |
anupam.sin |
1 |
/**
|
|
|
2 |
* Storage plugin
|
|
|
3 |
* Provides a simple interface for storing data such as user preferences.
|
|
|
4 |
* Storage is useful for saving and retreiving data from the user's browser.
|
|
|
5 |
* For newer browsers, localStorage is used.
|
|
|
6 |
* If localStorage isn't supported, then cookies are used instead.
|
|
|
7 |
* Retrievable data is limited to the same domain as this file.
|
|
|
8 |
*
|
|
|
9 |
* Usage:
|
|
|
10 |
* This plugin extends jQuery by adding itself as a static method.
|
|
|
11 |
* $.Storage - is the class name, which represents the user's data store, whether it's cookies or local storage.
|
|
|
12 |
* <code>if ($.Storage)</code> will tell you if the plugin is loaded.
|
|
|
13 |
* $.Storage.set("name", "value") - Stores a named value in the data store.
|
|
|
14 |
* $.Storage.set({"name1":"value1", "name2":"value2", etc}) - Stores multiple name/value pairs in the data store.
|
|
|
15 |
* $.Storage.get("name") - Retrieves the value of the given name from the data store.
|
|
|
16 |
* $.Storage.remove("name") - Permanently deletes the name/value pair from the data store.
|
|
|
17 |
*
|
|
|
18 |
* @author Dave Schindler
|
|
|
19 |
*
|
|
|
20 |
* Distributed under the MIT License
|
|
|
21 |
*
|
|
|
22 |
* Copyright (c) 2010 Dave Schindler
|
|
|
23 |
*
|
|
|
24 |
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
|
25 |
* of this software and associated documentation files (the "Software"), to deal
|
|
|
26 |
* in the Software without restriction, including without limitation the rights
|
|
|
27 |
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
|
28 |
* copies of the Software, and to permit persons to whom the Software is
|
|
|
29 |
* furnished to do so, subject to the following conditions:
|
|
|
30 |
*
|
|
|
31 |
* The above copyright notice and this permission notice shall be included in
|
|
|
32 |
* all copies or substantial portions of the Software.
|
|
|
33 |
*
|
|
|
34 |
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
|
35 |
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
|
36 |
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
|
37 |
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
|
38 |
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
|
39 |
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
|
|
40 |
* THE SOFTWARE.
|
|
|
41 |
*/
|
|
|
42 |
(function($) {
|
|
|
43 |
// Private data
|
|
|
44 |
var isLS=typeof window.localStorage!=='undefined';
|
|
|
45 |
// Private functions
|
|
|
46 |
function wls(n,v){var c;if(typeof n==="string"&&typeof v==="string"){localStorage[n]=v;return true;}else if(typeof n==="object"&&typeof v==="undefined"){for(c in n){if(n.hasOwnProperty(c)){localStorage[c]=n[c];}}return true;}return false;}
|
|
|
47 |
function wc(n,v){var dt,e,c;dt=new Date();dt.setTime(dt.getTime()+31536000000);e="; expires="+dt.toGMTString();if(typeof n==="string"&&typeof v==="string"){document.cookie=n+"="+v+e+"; path=/";return true;}else if(typeof n==="object"&&typeof v==="undefined"){for(c in n) {if(n.hasOwnProperty(c)){document.cookie=c+"="+n[c]+e+"; path=/";}}return true;}return false;}
|
|
|
48 |
function rls(n){return localStorage[n];}
|
|
|
49 |
function rc(n){var nn, ca, i, c;nn=n+"=";ca=document.cookie.split(';');for(i=0;i<ca.length;i++){c=ca[i];while(c.charAt(0)===' '){c=c.substring(1,c.length);}if(c.indexOf(nn)===0){return c.substring(nn.length,c.length);}}return null;}
|
|
|
50 |
function dls(n){return localStorage[n]? delete localStorage[n] : false;}
|
|
|
51 |
function dc(n){return wc(n,"",-1);}
|
|
|
52 |
|
|
|
53 |
/**
|
|
|
54 |
* Public API
|
|
|
55 |
* $.Storage - Represents the user's data store, whether it's cookies or local storage.
|
|
|
56 |
* $.Storage.set("name", "value") - Stores a named value in the data store.
|
|
|
57 |
* $.Storage.set({"name1":"value1", "name2":"value2", etc}) - Stores multiple name/value pairs in the data store.
|
|
|
58 |
* $.Storage.get("name") - Retrieves the value of the given name from the data store.
|
|
|
59 |
* $.Storage.remove("name") - Permanently deletes the name/value pair from the data store.
|
|
|
60 |
*/
|
|
|
61 |
$.extend({
|
|
|
62 |
Storage: {
|
|
|
63 |
set: isLS ? wls : wc,
|
|
|
64 |
get: isLS ? rls : rc,
|
|
|
65 |
remove: isLS ? dls :dc
|
|
|
66 |
}
|
|
|
67 |
});
|
|
|
68 |
})(jQuery);
|