The search processing with help JavaScript
readyState property XMLHttpRequest - numerical value which defines{determines} the status of a cycle of search. It changes from 0 for "uncertain" and up to 4 for "completed". Each time when varies readyState, appears event readystatechange and the operator is caused with the help onreadystatechange properties.
In listing 3 you have seen, how function getReadyStateHandler () for creation of the operator was caused. This operator then is attributed to property onreadystatechange. getReadyStateHandler () used that fact, that functions - objects of the first class in JavaScript. It designates, that functions can be parameters to other functions and can create and return values of other functions also. To a duty getReadyStateHandler () - to return value of function which checks, whether has come to the end XMLHttpRequest and whether the answer in a XML-format in the operator determined by a call has been sent. Listing 6 - a code for getReadyStateHandler ().
Listing 6. Function getReadyStateHandler ().
/*
* Returns function which expects that has ended
* Certain{Determined} XMLHttpRequest, then passes his{its} answer in a XML-format
* To the set operator * req is XMLHttpRequest, whose status changes
* responseXmlHandler - the function, transmitted search XML
*/
function getReadyStateHandler (req, responseXmlHandler) {
// Returns uncertain function which reads out
// Data XMLHttpRequest return function () {
// If the status is required "is finished"
if (req.readyState == 4) {
// We check, whether there has come the successful answer of the server
if (req.status == 200) {
// Passes XML to the operator
responseXmlHandler (req.responseXML);
} else {
// There was mistake HTTP
alert (" HTTP error: " +req.status);
}
}

}
}
|