16
Chapter 1
Let’s get back to updating Katie’s web report. First up, you need a function that creates
a new object to make requests to the server. This turns out to be a bit tricky, because
different browsers have different ways of creating this object. To help you out, we’ve
written some “pre-assembled” JavaScript for you. Whenever you see the logo,
it means you’ll have to take some code on faith, like this code below that creates an object
to make requests to the server. Trust us though—you’ll learn all about this code in more
detail in the chapters to come. For now, just type it in and see what it can do.
Pre-Assembled
JavaScript
n
n
Pre-Assembled
JavaScript
var request = null;
function createRequest() {
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!”);
}
Pre-Assembled
JavaScript
n
Step 1: Creating a request object
This line tries to create a new
request object.
These two lines try and create the
request object, too, but in a way that
works on Internet Explorer.
This is the type of the
request object.
...and we can spit out an error
message to users with JavaScript’s
alert() function.
Here’s a variable to hold the request object.
If something ...