-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest.js
71 lines (62 loc) · 1.77 KB
/
test.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
60
61
62
63
64
65
66
67
68
69
70
71
var storage = (function() {
if (window.localStorage) {
//alert('localStorage supported');
} else if (Cookies){
//alert("Local storage is not supported, using Cookies");
} else {
alert('exception');
}
var key = 'event';
return {
setLocalData: function(msg) {
var oldVal = '';
if (window.localStorage) {
oldVal = localStorage.getItem(key) || '';
localStorage.setItem(key, oldVal + msg);
} else if (Cookies) {
oldVal = Cookies.get(key) || '';
Cookies.set(key, oldVal + msg, { expires: Infinity });
}
},
getLocalData: function() {
if (window.localStorage) {
return localStorage.getItem(key) || '';
} else if (Cookies) {
return Cookies.get(key) || '';
}
},
removeLocalData: function() {
if (window.localStorage) {
localStorage.removeItem(key);
} else if (Cookies) {
Cookies.expire(key);
}
}
}
})();
// #1 beforeunload
window.onbeforeunload = function () {
storage.setLocalData("\n #1 onbeforeunload " + document.title + ' on ' + (new Date()).toString());
};
// # 2 pagehide
window.onpagehide = function () {
storage.setLocalData("\n #2 onpagehide " + document.title + ' on ' + (new Date()).toString());
};
// # 3 unload
window.onunload = function () {
storage.setLocalData("\n #3 onunload " + document.title + ' on ' + (new Date()).toString());
};
window.onload = function() {
storage.setLocalData("\nonload " + document.title + ' on ' + (new Date()).toString());
setInterval(showLog, 1000);
id('clearbtn').onclick = function() {
storage.removeLocalData();
showLog();
}
};
function id(str) {
return document.getElementById(str);
}
function showLog() {
id('log').value = storage.getLocalData() || '[empty]';
}