PageRenderTime 7ms CodeModel.GetById 2ms app.highlight 3ms RepoModel.GetById 1ms app.codeStats 0ms

/KitJs/src/js/localStorage.js

https://github.com/wuxq/KitJs
JavaScript | 88 lines | 54 code | 0 blank | 34 comment | 13 complexity | 32f57c90cba0a42f5544d970f6901aec MD5 | raw file
 1/**
 2 * 本地存储
 3 * @class $Kit.LocalStorage
 4 * @requires kit.js
 5 * @see <a href="https://github.com/xueduany/KitJs/blob/master/KitJs/src/js/localStorage.js">Source code</a>
 6 */
 7$Kit.LocalStorage = function() {
 8	//
 9	if(!window.localStorage) {
10		document.documentElement.style.behavior = 'url(#default#userData)';
11	}
12}
13$kit.merge($Kit.LocalStorage.prototype,
14/**
15 * @lends $Kit.LocalStorage.prototype
16 */
17{
18	/**
19	 * 保存
20	 * @param {Object}
21	 * @param {Object}
22	 * @param {Obejct}
23	 */
24	set : function(key, val, context) {
25		if(window.localStorage) {
26			return window.localStorage.setItem(key, val, context);
27		} else {
28			try {
29				document.documentElement.setAttribute(key, value);
30				return document.documentElement.save(context || 'default');
31			} catch (e) {
32			}
33		}
34	},
35	/**
36	 * 读取
37	 * @param {Object}
38	 * @param {Object}
39	 */
40	get : function(key, context) {
41		if(window.localStorage) {
42			return window.localStorage.getItem(key, context);
43		} else {
44			try {
45				document.documentElement.load(context || 'default');
46				return document.documentElement.getAttribute(key) || '';
47			} catch (e) {
48			}
49		}
50	},
51	/**
52	 * 删除
53	 * @param {Object}
54	 * @param {Object}
55	 */
56	rm : function(key, context) {
57		if(window.localStorage) {
58			return window.localStorage.removeItem(key, context);
59		} else {
60			try {
61				context = context || 'default';
62				document.documentElement.load(context);
63				document.documentElement.removeAttribute(key);
64				return document.documentElement.save(context);
65			} catch (e) {
66			}
67		}
68	},
69	/**
70	 * 清空
71	 */
72	clear : function() {
73		if(window.localStorage) {
74			return window.localStorage.clear();
75		} else {
76			try {
77				document.documentElement.expires = -1;
78			} catch (e) {
79			}
80		}
81	}
82});
83/**
84 * $Kit.LocalStorage的实例,直接通过这个实例访问$Kit.LocalStorage所有方法
85 * @global
86 * @type $Kit.LocalStorage
87 */
88$kit.localStorage = new $Kit.LocalStorage();