Java Reference
In-Depth Information
alert("Please enter an email address to check!");
return;
}
 
var url = "ch14_formvalidator.php?email=" + emailValue;
 
var request = new HttpRequest(url, handleResponse);
request.send();
}
This function also uses handleResponse() to handle the server's response. The handleResponse()
function executes when the HttpRequest object receives a complete response from the server. This
function uses the requested information to tell the user whether the username or e‐mail address is
available. Remember, the response from the server is JSON‐formatted data. So, you need to first parse
the data into a JavaScript object:
function handleResponse(responseText) {
var response = JSON.parse(responseText);
The server's response is parsed into an object that is stored in the response variable. You then use this
object's available property to display the appropriate message to the user:
if (response.available) {
alert(response.searchTerm + " is available!");
} else {
alert("We're sorry, but " + response.searchTerm + " is not available.");
}
}
If available is true , the function tells the user that his desired username or e‐mail address is okay to
use. If not, the alert box says that the user's desired username or e‐mail address is taken.
Finally, you need to set up the event listeners for your two links:
document.getElementById("usernameAvailability")
.addEventListener("click", checkUsername);
 
document.getElementById("emailAvailability")
.addEventListener("click", checkEmail);
You do this by simply retrieving the <a/> elements by their respective id values and listening for the
click event.
things to WatCh out for
Using JavaScript to communicate between server and client adds tremendous power to the
language's abilities. However, this power does not come without its share of caveats. The two most
important issues are security and usability.
 
Search WWH ::




Custom Search