forked from svn2github/AniDB
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathajax.js
164 lines (156 loc) · 5.75 KB
/
ajax.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
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
/* file XMLHttpRequest (AJAX) implementation
* @author fahrenheit ([email protected])
* @author paziel ()
* Some code derived from PetriW work at anidb
* version 1.0 (20.03.2007) - Initial Version
* version 1.1 (30.04.2008) - Some minor cleanup
* version 1.2 (24.04.2009) - Addition of paziek's XHR element, plus update of IE's MSXML methods.
*/
jsVersionArray.push({
"file":"ajax.js",
"version":"1.2",
"revision":"$Revision$",
"date":"$Date:: $",
"author":"$Author$",
"changelog":"Addition of paziek's XHR element, plus update of IE's MSXML methods"
});
// CORE Functions //
var isIE = false; // Are we dealing with an IE based browser?
var isHeaderNeeded = true;
var disableCaching = false; // Should we disable caching?
/* Function that returns a new XMLHttpRequest object
* @return an XMLHttpRequest object
*/
function xhttpRequest() {
var httpRequest;
if (window.XMLHttpRequest) { // Mozilla, Safari, ...
httpRequest = new XMLHttpRequest();
} else if (window.ActiveXObject) { // IE
isIE = true;
var progIDs = [ 'Msxml2.XMLHTTP.6.0', 'Msxml2.XMLHTTP.3.0', 'Msxml2.XMLHTTP', 'Microsoft.XMLHTTP' ]; // MSXML5.0, MSXML4.0 and Msxml2.DOMDocument all have issues
for (var i = 0; i < progIDs.length; i++) {
try {
return new ActiveXObject(progIDs[i]);
}
catch (ex) {
}
}
}
if (!httpRequest) {
if (seeDebug) alert('Error\nCould not create an XMLHTTP instance.');
return false;
}
return httpRequest;
}
/* Does the request
* @param obj The XMLHttpRequest object, or its wrapper (in that case, XHR should be in obj.xhr)
* @param url HTTP protcol URL
* @param handler Function that will handle the results
* @param override Overrides the document mime-type with given override
* @param data The Data to send to the URL
* @param method method
* @return void
*/
function doRequest(obj, url, handler, override, method, data) {
if (!obj) return;
try {
if(typeof obj.responseText === 'undefined') { // Checkes if we got XHR object, or its wrapper. Does this check works cross-browser? It should.
var xhr = obj.xhr;
} else var xhr = obj;
xhr.onreadystatechange = function() { xhttpRequestReadData(obj,handler,method); };
if (data) xhr.open('POST', url, true); // POST
else xhr.open('GET', url, true); // GET
if (xhr.overrideMimeType && override) xhr.overrideMimeType(override); // Setting the override type
if (isHeaderNeeded) xhr.setRequestHeader('X-LControl','x-no-cache'); // forces disabling of local caching
if (disableCaching) xhr.setRequestHeader('Cache-Control','no-cache'); // disables caching
if (data) {
xhr.setRequestHeader('Content-Type','application/x-www-form-urlencoded'); // Setting the correct header
xhr.send(data);
} else {
if (isIE) xhr.setRequestHeader('Content-Type','application/xml'); // IE compatibility
xhr.send(null);
}
} catch(e) {
alert('error: '+ e.description+'\nurl: '+url);
}
}
/* Fetches a given url and then passes the result to the given handler */
function xhttpRequestFetch(obj, url, handler, override, method) {
doRequest(obj, url, handler, override, method, null);
}
/* Posts given data to a given url and then passes the result to the given handler */
function xhttpRequestPost(obj, url, handler, data, override, method) {
doRequest(obj, url, handler, override, method, data)
}
/* This Function handles the status changes of the reply
* @param obj The XMLHttpRequest object
* @param handler The function that will handle the given obj
* @return void
*/
function xhttpRequestReadData(obj, handler, method) {
if (!obj) { xhttpRequestUpdateStatus('object is null...'); return; }
var rootDoc = 'root';
// try {
if(typeof obj.responseText === 'undefined') { // Checkes if we got XHR object, or its wrapper. Does this check works cross-browser? It should.
var xhr = obj.xhr;
} else var xhr = obj;
switch (xhr.readyState) {
case 2: xhttpRequestUpdateStatus('requesting data...',25); break;
case 3: xhttpRequestUpdateStatus('receiving data...',50); break;
case 4:
xhttpRequestUpdateStatus('data transfer completed...',100);
switch (xhr.status) {
case 0:
case 200:
case 304:
var data = null;
if (!method || method == 'xml') {
if (window.XMLHttpRequest) data = xhr.responseXML; // Mozilla and likes
else { // The original IE implementation
var progIDs = [ 'Msxml2.DOMDocument.6.0', 'Msxml2.DOMDocument.3.0']; // MSXML5.0, MSXML4.0 and Msxml2.DOMDocument all have issues
for (var i = 0; i < progIDs.length; i++) {
try {
data = new ActiveXObject(progIDs[i]);
data.loadXML(xhr.responseText);
break;
}
catch (ex) {
}
}
}
} else if (method == 'text') {
data = xhr.responseText;
}
if (handler) handler(data, obj);
break;
default:
alert('There was a problem with the request.\nServer returned: '+xhr.status+': '+xhr.statusText);
break;
}
break;
}
/* } catch ( e ) {
alert('error: '+ e.description);
}*/
}
/* This function is used to update the status of the Request
* @param text Text to show
* @param percentage Percentage done
* @return void
*/
function xhttpRequestUpdateStatus(text,percentage) {
try {
if (document.getElementById('statusBox')) {
if (globalStatus && globalStatus.container) {
globalStatus.loadingbar_color = 'red';
globalStatus.updateBarWithText(text,percentage,'Data request completion: ');
globalStatus.loadingbar_color = 'green';
} else {
var statusBox = document.getElementById('statusBox');
if (statusBox.firstChild) statusBox.removeChild(statusBox.firstChild);
statusBox.appendChild(document.createTextNode(text));
}
}
else window.status = text;
} catch (e) { }
}