-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest_ajax_sync.js
132 lines (117 loc) · 3.27 KB
/
test_ajax_sync.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
119
120
121
122
123
124
125
126
127
128
129
130
131
132
/**
* Send data via ajax before page closing
*/
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');
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();
};
id('testajax').onclick = function() {
ajax('testajax-sync');
}
};
var unloadHandler = function (e) {
e = e || window.event;
var orders = ['beforeunload', 'pagehide', 'unload'];
var type = e.type;
var index = indexOf(orders, type);
var msg = "\n #[" + (index + 1) + "] " + type + " " + document.title + ' on ' + (new Date()).toString();
storage.setLocalData(msg);
ajax(msg);
};
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;
}
function ajax(msg) {
var request = new XMLHttpRequest();
request.open('GET', './server/ajax.php?q=' + window.encodeURIComponent(msg), false);
request.onload = function() {
if (request.status >= 200 && request.status < 400) {
// Success!
//var data = JSON.parse(request.responseText);
storage.setLocalData("Ajax ok"+ ' on ' + (new Date()).toString());
} else {
// We reached our target server, but it returned an error
storage.setLocalData("Ajax failed"+ ' on ' + (new Date()).toString());
}
};
request.onerror = function() {
// There was a connection error of some sort
storage.setLocalData("Ajax error"+ ' on ' + (new Date()).toString());
};
request.send();
}