-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathutil.js
More file actions
75 lines (69 loc) · 2.24 KB
/
util.js
File metadata and controls
75 lines (69 loc) · 2.24 KB
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
'use strict';
// Answer provided by 'jolly.exe' in StackOverflow post
// http://stackoverflow.com/questions/901115/how-can-i-get-query-string-values-in-javascript
function getParameterByName(name, url) {
if (!url) url = window.location.href;
name = name.replace(/[\[\]]/g, "\\$&");
var regex = new RegExp("[?&]" + name + "(=([^&#]*)|&|#|$)"),
results = regex.exec(url);
if (!results) return null;
if (!results[2]) return '';
return decodeURIComponent(results[2].replace(/\+/g, " "));
}
// Taken from http://stackoverflow.com/questions/641857/javascript-window-resize-event
// Post by user Alex V
function AddEvent(object, type, callback) {
if (object == null || typeof(object) == 'undefined') return;
if (object.addEventListener) {
object.addEventListener(type, callback, false);
} else if (object.attachEvent) {
object.attachEvent("on" + type, callback);
} else {
object["on"+type] = callback;
}
};
function RemoveEvent(object, type, callback) {
if (object == null || typeof(object) == 'undefined') return;
if (object.removeEventListener) {
object.removeEventListener(type, callback, false);
} else if (object.detachEvent) {
object.detachEvent("on" + type, callback);
} else {
object["on"+type] = callback;
}
};
function LoadTextResource (url, cb) {
var xmlhttp = new XMLHttpRequest();
xmlhttp.open('GET', url, true);
xmlhttp.onload = function (e) {
if (xmlhttp.status < 200 || xmlhttp.status >= 300) {
console.error('ERROR loading text resource', url, xmlhttp.status, xmlhttp.statusText);
cb(new Error(xmlhttp.statusText));
} else {
cb(null, xmlhttp.responseText);
}
};
xmlhttp.onerror = cb;
xmlhttp.send();
};
function LoadJSONResource (url, cb) {
LoadTextResource(url, function (err, res) {
if (err) {
cb(err);
} else {
try {
var obj = JSON.parse(res);
cb(null, obj);
} catch (e) {
cb(e);
}
}
});
};
function LoadImage (url, cb) {
var image = new Image();
image.onload = function () {
cb (null, image);
};
image.src = url;
};