-
Notifications
You must be signed in to change notification settings - Fork 0
/
one-session.js
59 lines (54 loc) · 1.94 KB
/
one-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
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
/*onSession 1.1.3*/
(function (root, factory) {
if (typeof define === 'function' && (define.amd || define.cmd)) {
define(function (require, exports, module) {
return factory(root);
});
} else {
root.elperBase = factory(root);
}
})(this, function (root) {
// 是否处于隐私模式下
var isPrivacy = (function isPrivacy() {
var testKey = 'test',
storage = root.sessionStorage;
try {
storage.setItem(testKey, 'testValue');
storage.removeItem(testKey);
return true;
} catch (error) {
return false;
}
})();
var oneSession = {
get: function (name) {
if (root.sessionStorage && isPrivacy) {
var sessionVal = sessionStorage.getItem(name);
sessionStorage.removeItem(name);
return sessionVal;
} else {
if (document.cookie.length > 1) {
var c_start = document.cookie.indexOf(name + "=");
if (c_start != -1) {
c_start = c_start + name.length + 1;
var c_end = document.cookie.indexOf(";", c_start);
if (c_end == -1) c_end = document.cookie.length;
return decodeURI(document.cookie.substring(c_start, c_end));
}
}
return "";
}
},
set: function (name, value, expiredays) {
if (root.sessionStorage && isPrivacy) {
sessionStorage.setItem(name, value);
} else {
var exdate = new Date();
exdate.setSeconds(exdate.getSeconds() + expiredays);
document.cookie = name + "=" + decodeURIComponent(value) +
((expiredays == null) ? "" : ";expires=" + exdate.toGMTString());
}
}
};
return oneSession;
});