If you've been using the web of late you'll have noticed some funky things being done with client side HTML that pulls data from a server without the need to refresh the page. Such techniques are used in pages like Google's Suggest, (www.google.com/webhp?complete=1&hl=en) Maps (maps.google.com/)and Gmail (www.gmail.com), along with a number of other applications from web professionals trying out the technology. It's similar to how Flash can pull XML data and utilise it, but using plain old HTML and JavaScript.
In this tutorial we're going to look at the basics of how this is done using XMLHttpRequest, and show a simple application you can put it to.
To use this tutorial you'll need an understanding of JavaScript and basic XML and PHP, if you're only familiar with server behaviors the tutorial is probably a bit advanced for you. I've tried to keep the example as un-cluttered as possible to focus on the core idea behind the technology.
How it works
Normally when you request a web page, your browser sends a request for the page and then the page is returned. The browser only interacts with the server at the page request, so our only way to get an update to the page based on user data is to request a new one or use a plug-in like Flash or Java.
XMLHttpRequest allows us to request additional data from the server via JavaScript. A series of calls to the XMLHttpRequest object allow us to request small batches of XML data from a server source over a web connection. Because they're small, the technique is ideal for small dynamic updates to page information. For security reasons these requests can only go to the server that the JavaScript was originally downloaded from.
The technology was originally created by Microsoft as an Active X object, but other browsers have followed suit with a native JavaScript Object. This object is actually part of the W3C standard, but is obscurely hidden within the depths of the SVG specification.
Read More