you’re on your way 4
189
asynchronous applications
Creating two request objects
First, we need to actually create both request objects. Open up
ajax.js and make the following changes, so that we’re creating
two request objects instead of just one:
var request = null;
function createRequest() {
var request = null;
try {
request = new XMLHttpRequest();
} catch (trymicrosoft) {
try {
request = new ActiveXObject(“Msxml2.XMLHTTP”);
} catch (othermicrosoft) {
try {
request = new ActiveXObject(“Microsoft.XMLHTTP”);
} catch (failed) {
request = null;
}
}
}
if (request == null) {
alert(“Error creating request object!”);
} else {
return request;
}
}
var request1 = createRequest();
var request2 = createRequest();
ajax.js
We no longer want to create a single
request object. Delete this line.
Now, turn this code back into a
function, so we can run it more
than once easily.
If the request object is created
successfully, return it as the result
of the function.
Finally, in static JavaScript, create two
request objects. Assign each the return
value of the createRequest() function.
This is your ajax.js le.
When this nishes running, you’ll have
two request objects-request1 and
request2-created and ready to use.