-
Notifications
You must be signed in to change notification settings - Fork 0
/
testHtml.html
87 lines (66 loc) · 2.39 KB
/
testHtml.html
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
<!--
* @Author : ZhouQiJun
* @Date : 2023-10-13 14:20:32
* @LastEditors : ZhouQiJun
* @LastEditTime: 2023-10-16 11:40:55
* @Description :
https://www.chensheng.group/2017/11/23/46-SessionStorage%E7%9A%84%E5%85%B1%E4%BA%AB/
-->
<!DOCTYPE html>
<html>
<head>
<title>Sharing sessionStorage between tabs for secure multi-tab authentication</title>
</head>
<body>
<h3><a href="">sessionStorage</a></h3>
<h3 id="stData"></h3>
<button id="btnSet">Set session storage</button>
<button id="btnClear">Clear session storage</button>
<script type="text/javascript">
// Removed IE support in this demo for the sake of simplicity
(function() {
if (!sessionStorage.length) {
// Ask other tabs for session storage
localStorage.setItem('getSessionStorage', Date.now());
};
window.addEventListener('storage', function(event) {
//console.log('storage event', event);
if (event.key == 'getSessionStorage') {
// Some tab asked for the sessionStorage -> send it
localStorage.setItem('sessionStorage', JSON.stringify(sessionStorage));
localStorage.removeItem('sessionStorage');
} else if (event.key == 'sessionStorage' && !sessionStorage.length) {
// sessionStorage is empty -> fill it
var data = JSON.parse(event.newValue),
value;
for (key in data) {
sessionStorage.setItem(key, data[key]);
}
showSessionStorage();
}
});
window.onbeforeunload = function() {
//sessionStorage.clear();
};
/* This code is only for the UI in the demo, it's not part of the solution */
var el;
function showSessionStorage() {
el.innerHTML = sessionStorage.length ? JSON.stringify(sessionStorage) : 'sessionStorage is empty';
}
window.addEventListener('load', function() {
el = document.getElementById('stData');
showSessionStorage();
document.getElementById('btnSet').addEventListener('click', function() {
sessionStorage.setItem('token', '123456789');
showSessionStorage();
})
document.getElementById('btnClear').addEventListener('click', function() {
sessionStorage.clear();
showSessionStorage();
})
})
})();
</script>
</body>
</html>