Skip to content

Instantly share code, notes, and snippets.

@iadvize
Forked from FGRibreau/strophe.xdomainrequest.js
Created January 7, 2011 20:28
Show Gist options
  • Save iadvize/770052 to your computer and use it in GitHub Desktop.
Save iadvize/770052 to your computer and use it in GitHub Desktop.
Strophe.addConnectionPlugin("xdomainrequest", {
init: function() {
if (window.XDomainRequest) {
Strophe.debug("using XdomainRequest for IE");
// override thee send method to fire readystate 2
XDomainRequest.prototype.send = (function(oldSend) {
return function() {
oldsend.apply(this, arguments);
this.readyState = 2;
try {
this.onreadystatechange();
} catch(e) {}
};
})(XDomainRequest.prototype.send);
// replace Strophe.Request._newXHR with the xdomainrequest version
Strophe.Request.prototype._newXHR = function() {
var fireReadyStateChange = function(xhr, status) {
xhr.status = status;
xhr.readyState = 4;
try {
xhr.onreadystatechange();
} catch(e) {}
};
var xhr = new XDomainRequest();
xhr.readyState = 0;
xhr.onreadystatechange = this.func.prependArg(this);
xhr.onload = function() {
xmlDoc = new ActiveXObject("Microsoft.XMLDOM");
xmlDoc.async = "false";
xmlDoc.loadXML(xhr.responseText);
xhr.responseXML = xmlDoc;
fireReadyStateChange(xhr, 200);
};
xhr.onerror = function() {
fireReadyStateChange(xhr, 500);
};
xhr.ontimeout = function() {
fireReadyStateChange(xhr, 500);
};
return xhr;
}
} else {
Strophe.error("XDomainRequest not found. Falling back to native XHR implementation.");
}
}
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment