Skip to Content
Head Rush Ajax
book

Head Rush Ajax

by Brett McLaughlin
March 2006
Beginner to intermediate
448 pages
13h 33m
English
O'Reilly Media, Inc.
Content preview from Head Rush Ajax
you’re on your way 4
55
next generation applications
function updatePage() {
if (request.readyState == 4) {
var newTotal = request.responseText;
var boardsSoldEl = document.getElementById(“boards-sold”);
var cashEl = document.getElementById(“cash”);
replaceText(boardsSoldEl, newTotal);
/* Figure out how much cash Katie has made */
...
/* Update the cash total on the web form */
...
}
}
Here’s
that code
you wrote
a few
pages back.
When the request object’s
readyState is set to 4, the
server is done with the request.
Don’t forget the closing bracket.
You already know that the browser will run your updatePage() function when it gets a response
from the server. But, there’s a twist: the browser actually runs updatePage() every time the ready
state changes, and not just when the server is nished sending back a response. Look at the diagram
on page 54 again, and notice that as the request is progressing, the ready state number changes.
It looks like when the server is nished with our request, the ready state is “4”, so we can check for
that number in our code. That way, we can make sure that we only try and update Katie’s report
when we know that the server has sent us back a response.
Checking for the right ready state
readyState is the property on the request
object that stores the current ready state.
This function
will get run
every time
the ready
state changes.
When the serv
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Start your free trial

You might also like

What Employees Want Most in Uncertain Times

What Employees Want Most in Uncertain Times

Kristine W. Powers, Jessica B.B. Diaz
What Successful Project Managers Do

What Successful Project Managers Do

W. Scott Cameron, Jeffrey S. Russell, Edward J. Hoffman, Alexander Laufer
How to Become a Game-Changing Leader

How to Become a Game-Changing Leader

Douglas A. Ready, Alan Mulally

Publisher Resources

ISBN: 0596102259Errata PageSupplemental Content