-
Notifications
You must be signed in to change notification settings - Fork 1
/
sprinkles.js
62 lines (57 loc) · 1.5 KB
/
sprinkles.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
// FIXME: credit this snippet
var AJAX = function() {
// (IE) XMLHttpRequest is an ActiveXObject in IE
return {
isLoaded : 2,
isComplete : 4,
newRequest : function() {
var http_request;
if(window.XMLHttpRequest) { // native XMLHttpRequest object (FireFox, etc.)
try {
http_request = new XMLHttpRequest();
} catch(e) {
throw ("Failed to create (native) XMLHttpRequest");
}
} else if(window.ActiveXObject) { //IE
try {
http_request = new ActiveXObject("Msxml2.XMLHTTP");
} catch(e) {
try {
http_request = new ActiveXObject("Microsoft.XMLHTTP");
} catch(e) {
throw ("Failed to create (ActiveX) XMLHttpRequest object");
}
}
}
return http_request;
}
};
}();
function getHTMLAJAX(url) {
// TBD: async
var req = AJAX.newRequest();
req.open('GET', url, true);
var result;
req.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
req.send();
return req.responseText;
}
var LOADED = 4;
function getHTMLAJAXAsync(url, kappa) {
// TBD: async
var req = AJAX.newRequest();
req.open('GET', url, true);
var result;
req.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
req.onreadystatechange = function() {
if (req.readyState == LOADED) {
kappa(req.responseText);
}
}
req.send(null);
}
function trim(str) {
var result = str.replace(/^[\n\r\t]+/, '');
result = result.replace(/[\n\r\t]+$/, '');
return result;
}