-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutil.js
34 lines (32 loc) · 946 Bytes
/
util.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
var loadTextResource = function (url, callback) {
var request = new XMLHttpRequest();
request.open('GET', url + '?please-dont-cache=' + Math.random(), true);
request.onload = function () {
if (request.status < 200 || request.status > 299) {
callback('Error: HTTP Status ' + request.status + ' on resource ' + url);
} else {
callback(null, request.responseText);
}
};
request.send();
};
var loadImage = function (url, callback) {
var image = new Image();
image.onload = function () {
callback(null, image);
};
image.src = url;
};
var loadJSONResource = function (url, callback) {
loadTextResource(url, function (err, result) {
if (err) {
callback(err);
} else {
try {
callback(null, JSON.parse(result));
} catch (e) {
callback(e);
}
}
});
};