The XMLHttpRequest Object / XML Requests via JavaScript
With an HTTP request, a web page can make a request to, and get a response from
a web server - without reloading the page. The user will stay on the same page,
and he or she will not notice that scripts might request pages, or send data to
a server in the background.
By using the XMLHttpRequest object, a web developer can change a page with data
from the server after the page has loaded.
Creating an XMLHttpRequest Object
For Mozilla, Firefox, Safari, Opera, and Netscape:
var xmlhttp = new XMLHttpRequest()
For Internet Explorer:
var xmlhttp = new ActiveXObject("Microsoft.XMLHTTP")
--------------------------------------------------------------------------------
Creating an XMLHttpRequest Object Example (cross browser):
Dynamic Usage within HTML
Note:
An important property in the example above is the onreadystatechange property.
This property is an event handler which is triggered each time the state of the
request changes. The states run from 0 (uninitialized) to 4 (complete). By
having the function xmlhttpChange() check for the state changing, we can tell
when the process is complete and continue only if it has been successful.
All the examples here use the async mode (the third parameter of open() set to
true). The async parameter specifies whether the request should be handled
asynchronously or not. True means that script continues to run after the send()
method, without waiting for a response from the server. false means that the
script waits for a response before continuing script processing. By setting
this parameter to false, you run the risk of having your script hang if there
is a network or server problem, or if the request is long (the UI locks while
the request is being made) a user may even see the "Not Responding" message.
It is safer to send asynchronously and design your code around the
onreadystatechange event!
--------------------------------------------------------------------------------
Notes From: http://www.w3.org/TR/XMLHttpRequest/
Use setRequestHeader() after open() and before send().
The send() flag is only relevant when the state is open (readyState=1).
Even when async is set to false the readystatechange event will still be
dispatched.
For the send() method, if data is a DOMString, data must be encoded as UTF-8
for transmission. If data is a DOCUMENT, data must be serialized into a
namespace well-formed XML document and encoded using the encoding given by
data.xmlEncoding, if specified, or UTF-8 otherwise. If this fails because the
document cannot be serialized user agents must act as if data was null. If data
is not a DOMString or DOCUMENT, the stringification mechanisms of the host
language must be used on data and the result must be treated as if data is a
DOMString.
Invoking send() without the data argument must give the same result as if it was
invoked with null as argument.
Scripts should specify the Content-Type header via setRequestHeader before
invoking send() with an argument. If the argument to send() is a Document and no
Content-Type header has been set user agents must set it to application/xml for
XML documents and to the most appropriate media type for other documents (using
intrinsic knowledge about the document).
If the response is an HTTP redirect (status code 301, 302, 303 or 307), then it
must be transparently followed (unless it violates security, infinite loop
precautions or the scheme isn't supported).
Once the request has been successfully acknowledged the state must be set to
sent. Immediately before receiving the message body (if any), the state must be
set to to receiving. When the request has completed loading, the state must be
set to loaded.
--------------------------------------------------------------------------------
Another function to return a cross-browser XMLHTTP Object:
function getXMLHttpObj(){
if(typeof(XMLHttpRequest)!='undefined')
return new XMLHttpRequest();
var axO=['Msxml2.XMLHTTP.6.0', 'Msxml2.XMLHTTP.4.0',
'Msxml2.XMLHTTP.3.0', 'Msxml2.XMLHTTP', 'Microsoft.XMLHTTP'], i;
for(i=0;i