forked from GamesCrafters/GamesmanGames
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsession.js
40 lines (29 loc) · 879 Bytes
/
session.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
if (JSON && JSON.stringify && JSON.parse) var Session = Session || (function() {
// window object
var win = window.top || window;
// session store
var store = (win.name ? JSON.parse(win.name) : {});
// save store on page unload
function Save() {
win.name = JSON.stringify(store);
};
// page unload event
if (window.addEventListener) window.addEventListener("unload", Save, false);
else if (window.attachEvent) window.attachEvent("onunload", Save);
else window.onunload = Save;
// public methods
return {
// set a session variable
set: function(name, value) {
store[name] = value;
},
// get a session value
get: function(name) {
return (store[name] ? store[name] : undefined);
},
// clear session
clear: function() { store = {}; },
// dump session data
dump: function() { return JSON.stringify(store); }
};
})();