-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest2.js
118 lines (104 loc) · 2.87 KB
/
test2.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
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
109
110
111
112
113
114
115
116
117
118
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);
}
}
}
})();
var logarea = id('log');
/*
// #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());
};
*/
var loadHandler = function(e) {
e = e || window.event;
storage.setLocalData("\n " + e.type + " " + document.title + ' on ' + (new Date()).toString());
showLog();
setInterval(showLog, 1000);
id('clearbtn').onclick = function() {
storage.removeLocalData();
showLog();
};
};
var unloadHandler = function (e) {
e = e || window.event;
var orders = ['beforeunload', 'pagehide', 'unload'];
var type = e.type;
var index = indexOf(orders, type);
storage.setLocalData("\n #[" + (index + 1) + "] " + type + " " + document.title + ' on ' + (new Date()).toString());
};
if ('onpageshow' in window) {
addEvent(window, 'pageshow', loadHandler);
addEvent(window, 'pagehide', unloadHandler);
} else {
addEvent(window, 'load', loadHandler);
addEvent(window, 'unload', unloadHandler);
}
function addEvent(target, type, listener) {
if (window.addEventListener) {
target.addEventListener(type, listener, false);
} else {
target.attachEvent('on' + type, listener);
}
}
function id(str) {
return document.getElementById(str);
}
function showLog() {
logarea.value = storage.getLocalData() || '[empty]';
if (logarea.scrollTo) {
logarea.scrollTo({
top: logarea.scrollHeight
});
}
}
function indexOf(arr, searchElement) {
if (Array.prototype.indexOf) {
return arr.indexOf(searchElement);
}
for (var i = arr.length - 1; i >= 0; i--) {
if (arr[i] === searchElement) {
return i;
}
}
return -1;
}