Skip to content

Commit 4648eba

Browse files
author
RyLee Harrison
committed
++
1 parent 7642946 commit 4648eba

1 file changed

Lines changed: 55 additions & 0 deletions

File tree

code/utils/Session.js

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
const Session = (() => {
2+
const win = window.top || window;
3+
4+
try {
5+
let store = (win.name ? JSON.parse(win.name) : {});
6+
7+
function Save() {
8+
win.name = JSON.stringify(store);
9+
}
10+
11+
if (window.addEventListener) {
12+
window.addEventListener('unload', Save, false);
13+
} else if (window.attachEvent) {
14+
window.attachEvent('onunload', Save);
15+
} else {
16+
window.onunload = Save;
17+
}
18+
19+
return {
20+
set(name, value) {
21+
store[name] = value;
22+
},
23+
get(name) {
24+
return (store[name] ? store[name] : undefined);
25+
},
26+
clear() {
27+
store = {};
28+
},
29+
dump() {
30+
return JSON.stringify(store);
31+
}
32+
};
33+
} catch (e) {
34+
win.name = ''; /* reset cache */
35+
36+
return {
37+
set() {},
38+
get() {},
39+
clear() {},
40+
dump() {}
41+
};
42+
}
43+
})();
44+
45+
Session.set('user', {
46+
name: 'RyLee Harrison'
47+
});
48+
49+
console.log( // ===> '{"user":{"name":"RyLee Harrison"}}'
50+
Session.dump()
51+
);
52+
53+
console.log( // ===> '{name: "RyLee Harrison"}'
54+
Session.get('user')
55+
);

0 commit comments

Comments
 (0)