forked from jsbin/jsbin
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstorage.js
More file actions
58 lines (52 loc) · 1.61 KB
/
storage.js
File metadata and controls
58 lines (52 loc) · 1.61 KB
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
// I would like to lazy load json2, but I'm worried we're trying to access
// JSON very early in the code
//= require <json2>
var requiresCookies = (function () {
var ua = navigator.userAgent.toLowerCase(),
id = null;
if (/mozilla/.test(ua) && !/compatible/.test(ua)) {
id = new Date().getTime();
document.cookie = '__cprobe=' + id + ';path=/';
if (document.cookie.indexOf(id,0) === -1) {
return true;
}
}
return false;
})();
// Firefox with Cookies disabled triggers a security error when we probe window.sessionStorage
// currently we're just disabling all the session features if that's the case.
var sessionStorage = window.sessionStorage;
var localStorage = window.localStorage;
if (!requiresCookies && window.sessionStorage) {
sessionStorage = window.sessionStorage;
}
if (!sessionStorage) {
sessionStorage = (function () {
var data = window.top.name ? JSON.parse(window.top.name) : {};
return {
clear: function () {
data = {};
window.top.name = '';
},
getItem: function (key) {
return data[key] || null;
},
removeItem: function (key) {
delete data[key];
window.top.name = JSON.stringify(data);
},
setItem: function (key, value) {
data[key] = value;
window.top.name = JSON.stringify(data);
}
};
})();
}
if ((!requiresCookies && !window.localStorage) || requiresCookies) {
// dirty, but will do for our purposes
localStorage = sessionStorage;
} else if (!requiresCookies) {
localStorage = window.localStorage;
} else if (!localStorage) {
localStorage = sessionStorage;
}