Main Page

Practical Uses

}, 10);
} else {
alert(“Your browser doesn’t support HTTP requests.”);
}
};
Using this method, it’s now possible to perform POST requests in a variety of browsers using the same
code:
var sURL = “http://www.somewhere.com/page.php”;
var sParams = “”;
sParams = addPostParam(sParams, “name”, “Nicholas”);
sParams = addPostParam(sParams, “book”, “Professional JavaScript”);
oHttp.post(sURL, function(sData) {
alert(“Server sent back: “ + sData);
});
Once again, always supply the full URL when using this method.
Practical Uses
Although all this client-server communication stuff is really cool, it also has some practical applications.
You can, for example, create a search page that never unloads. It simply makes a request to get new data
for each new search you submit. This is the model used by Amazon.com’s new Web search engine, A9
(
http://www.a9.com
).
After your first A9 search, you can choose to add images, movies, books, and other information to your
search results by clicking on the various options on the right of the screen. As you do, a new area in the
page opens up, and new search results are loaded using the hidden iframe technique discussed earlier in
the chapter.
Several Web sites use JavaScript client-server communication to provide search results. One such site,
the Bitflux blog (
http://blog.bitflux.ch/
), uses a XML HTTP request objects for its
LiveSearch
capa-
bility. This cool technique uses the
onkeypress
event handler to detect what the user types into the
search box. For each new letter entered into the text box, the site does a search and displays its results in
a layer directly on the page, without ever reloading the page.
Another good use for this type of communication is to provide functionality that isn’t available in
JavaScript. For instance, you may want to validate some data the user has entered against a database.
Using the
onblur
event handler, you can make a request back to the database to determine if the value
is valid, and then present an error message to the user if is the data is not valid. The possibilities really
are endless.
506
Chapter 16
19_579088 ch16.qxd 3/28/05 11:42 AM Page 506


JavaScript EditorFree JavaScript Editor     Ajax Editor


©