Game Development Reference
In-Depth Information
One example of how using a JavaScript library, particularly jQuery, can save you de-
velopment time and effort is trying to make an asynchronous request to your server.
Without jQuery, there is a bit of boilerplate code that we'd need to write so that differ-
ent browsers all behave the same. The code is as follows:
var xhr = null;
// Attempt to create the xhr object the popular
way
try {
xhr = new XMLHttpRequest();
}
// If the browser doesn't support that
construct, try a different one
catch (e) {
try {
xhr = new
ActiveXObject("Microsoft.XMLHTTP");
}
// If it still doesn't support the previous 2
xhr constructs, just give up
catch (e) {
throw new Error("This browser doesn't
support AJAX");
}
// If we made it this far, then the xhr object
is set, and the rest
// of the API is identical independent of which
version we ended up with
xhr.open("GET", "//www.some-website.com", true);
xhr.onreadystatechange = function(response) {
// Process response
// (...)
};
xhr.send();
Search WWH ::




Custom Search