118
Chapter 2
So to get around the caching, we have to
change our request URL every time. But... if
we’re talking to the same script, and we’re
not sending in data in the Boards app, how
can we make the URL different?
The browser doesn’t care how the request URL
is different; it just cares that it’s different. So, we
can add a dummy parameter onto the request
URL, and give it a different value every time
we send the request. And, since we don’t want
to write a random number generator, or do any
extra work, we can just grab the current time (in
seconds), and add that to the request URL.
Let’s take a look at how we can x up
getBoardsSold() from Chapter 1:
Sometimes it takes a hack...
function getBoardsSold() {
createRequest();
var url = “getUpdatedBoardSales-ajax.php”;
url = url + “?dummy=” + new Date().getTime();
request.open(“GET”, url, true);
request.onreadystatechange = updatePage;
request.send(null);
}
Remember back in the
old days, when our
request object was
created in a function?
This rst line sets the
URL normally, and the
second line adds a dummy
value to the URL.
Our dummy
parameter has
the current time
as its value.
Now the request URL changes...
getUpdatedBoardSales-ajax.php?dummy=1139262723388
getUpdatedBoardSales-ajax.php?dummy=1139262774440
getUpdatedBoardSales-ajax.php?dummy=1139262797519
...because each time the request URL
is built, the time is slightly different.
Now, no ...