Ajax JavaScript CSS HTML 5 Firefox

XDomainRequest object for cross-sites Ajax

The XDomainRequest object is not standard. It has been created by Microsoft and implemented in Internet Explorer 8 and 9 for exchanging data between domains, just as the XMLHttpRequest object exchanges data between the browser and the server on a single domain.

Here is the current specification of the XDomainRequest object.



This property contains the type of document. When you send data, you have to assigns the property according to the document that is sent. When you load a document, this property contains the type of file which was loaded.

xdr = new XDomainRequest(); 
xdr.contentType = "text/plain";


Contains the contents of a file you just load. We must wait until the onload event happened to read the contents of this variable.



Contains the maximum time allocated to complete the request. (Probably in milliseconds, this is not specified).

xdr.timeout = 1200:



Triggered when the action can not succeed. A function is assigned to the event to be executed when it is raised.

function problem()
    alert("Error in the request");
xdr.onerror = problem;


Triggered when loading is completed. To use it, assigns a function to it and this function reads the content of the attribute responseText, which when it is fully assigned causes the event triggered.

function loading()
xdr.onload = loading;   


Fired at intervals during the loading of a document on the remote site. Created to display a progress bar or inform on the progress of the request in any way.

var x = 0;
function progress()

xdr.onprogress = progress;

In this example, it is assumed that bar is a JavaScript object that displays the progress. We can base the remaining time on the content of responseText because it is partially assigned during loading (and one can know the size of the file to load elsewhere).


Triggered when the time limit for loading (defined by the attribute timeout) is exceeded before the end of loading, and thus before the outbreak of the onload event.

Methods of XDomainRequest


Open a connection with the server by defining a method and a file.
The method can be one of the HTTP method that follow:

GET to load a document.

POST to send data to the server.

xdr.open(method, url); 


Send data or launch a request to load a file. With POST, contains data to send as parameter, in GET mode has no parameter or the empty string "".



This method can be called during the execution of the request to cancel it. It may is associated to a button of the interface to allow users to cancel the operation.



Complete code for a GET request with XDomainRequest.

xdr = new XDomainRequest(); 

xdr.open("GET", "http://www.anotheresite.com/myfile.txt");

Complete code for a POST request.

xdr = new XDomainRequest(); 

xdr.open("POST", "http://www.anothersite.com/script.php");
xdr.send("données à envoyer"); 


There is no major difference between the interfaces of XDomainRequest and that of its predecessor, the XMLHttpRequest object (originally created by Microsoft as well). It should be noted, however, two useful additions: ontimeout and onprogress.

Another difference too, the onload event which replaces onreadystatechange and readyState and in fact simplifies the use of the object.
However onload and onprogress are planned for the Level 2 of the specification of XHR.

You should not use this object for a public usage at this time. On the one hand, the definition is not complete, on the other hand, it only works on Internet Explorer 8 and 9 and is replaced by XHR 2 on IE 10. You can use a substitute as described in the article Cross-site Ajax.

More information

© 2008-2013 Xul.fr