Repository navigation
Expand file tree
/
Copy pathlocalstorage.js
More file actions
108 lines (102 loc) · 2.85 KB
/
Copy pathlocalstorage.js
File metadata and controls
108 lines (102 loc) · 2.85 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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
/* jshint -W020, unused: false, noempty: false, boss: true */
/*
* Implement localStorage to support Firefox 2-3 and IE 5-7
*/
var localStorage; // jshint ignore:line
if (!BUILD_COMPAT_LOCAL_STORAGE) {
localStorage = window.localStorage;
}
if (BUILD_COMPAT_LOCAL_STORAGE) {
// test that Window.localStorage is available and works
const windowLocalStorageAvailable = () => {
var uid = new Date();
var result;
try {
window.localStorage.setItem(uid, uid);
result = window.localStorage.getItem(uid) === String(uid);
window.localStorage.removeItem(uid);
return result;
} catch (e) {
// localStorage not available
}
return false;
};
if (windowLocalStorageAvailable()) {
localStorage = window.localStorage;
} else if (window.globalStorage) {
// Firefox 2-3 use globalStorage
// See https://developer.mozilla.org/en/dom/storage#globalStorage
try {
localStorage = window.globalStorage[window.location.hostname];
} catch (e) {
// Something bad happened...
}
} else if (typeof document !== 'undefined') {
// IE 5-7 use userData
// See http://msdn.microsoft.com/en-us/library/ms531424(v=vs.85).aspx
var div = document.createElement('div'),
attrKey = 'localStorage';
div.style.display = 'none';
document.getElementsByTagName('head')[0].appendChild(div);
if (div.addBehavior) {
div.addBehavior('#default#userdata');
localStorage = {
length: 0,
setItem: function(k, v) {
div.load(attrKey);
if (!div.getAttribute(k)) {
this.length++;
}
div.setAttribute(k, v);
div.save(attrKey);
},
getItem: function(k) {
div.load(attrKey);
return div.getAttribute(k);
},
removeItem: function(k) {
div.load(attrKey);
if (div.getAttribute(k)) {
this.length--;
}
div.removeAttribute(k);
div.save(attrKey);
},
clear: function() {
div.load(attrKey);
var i = 0;
var attr;
while (attr = div.XMLDocument.documentElement.attributes[i++]) {
div.removeAttribute(attr.name);
}
div.save(attrKey);
this.length = 0;
},
key: function(k) {
div.load(attrKey);
return div.XMLDocument.documentElement.attributes[k];
}
};
div.load(attrKey);
localStorage.length = div.XMLDocument.documentElement.attributes.length;
} else {
/* Nothing we can do ... */
}
}
if (!localStorage) {
localStorage = {
length: 0,
setItem: function(k, v) {
},
getItem: function(k) {
},
removeItem: function(k) {
},
clear: function() {
},
key: function(k) {
}
};
}
}
export default localStorage;