Skip to content

Commit 9070f7c

Browse files
committed
Finished Chapter 14 exercise 1
1 parent 883c196 commit 9070f7c

File tree

1 file changed

+27
-0
lines changed

1 file changed

+27
-0
lines changed

ch14exercise1.js

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
function HttpRequest(url, callback, async) {
2+
this.request = new XMLHttpRequest();
3+
this.request.open("GET", url, async);
4+
var tempRequest = this.request;
5+
function reqReadyStateChange() {
6+
if (tempRequest.readyState == 4) {
7+
if (tempRequest.status == 200) {
8+
callback(tempRequest.responseText);
9+
} else {
10+
alert("An error occurred trying to contact the server.");
11+
}
12+
}
13+
}
14+
if (async) {
15+
this.request.onreadystatechange = reqReadyStateChange;
16+
} else {
17+
this.request.send(null);
18+
if (tempRequest.status == 200) {
19+
callback(tempRequest.responseText);
20+
} else {
21+
alert("An error occurred trying to contact the server.");
22+
}
23+
}
24+
}
25+
HttpRequest.prototype.send = function () {
26+
this.request.send(null);
27+
};

0 commit comments

Comments
 (0)