Skip to content

Instantly share code, notes, and snippets.

@devote
Created April 2, 2014 13:17
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save devote/9933899 to your computer and use it in GitHub Desktop.
Save devote/9933899 to your computer and use it in GitHub Desktop.
XDomainRequest polyfill for HTML4 browsers (IE7, IE6, etc.)
/*
* XDomainRequest polyfill for HTML4 browsers (IE7, IE6, etc.) v0.1.0
*
* Required: jClass 1.4.0+
*
* Copyright 2012-2014, Dmitriy Pakhtinov ( spb.piksel@gmail.com )
*
* http://spb-piksel.ru/ - https://github.com/devote
*
* Dual licensed under the MIT and GPL licenses:
* http://www.opensource.org/licenses/mit-license.php
* http://www.gnu.org/licenses/gpl.html
*
* Update: 02-04-2014
*/
window.XDomainRequest = (function(window) {
var
document = window.document,
documentElement = document.documentElement;
function addEvent(elem, type, fn) {
if (elem.addEventListener) {
elem.addEventListener(type, fn, false);
} else {
elem.attachEvent("on" + type, fn);
}
}
function removeEvent(elem, type, fn) {
if (elem.removeEventListener) {
elem.removeEventListener(type, fn, false);
} else {
elem.detachEvent("on" + type, fn);
}
}
return window.XDomainRequest || jClass({"compact": 1}, function() {
var iframe,
iframeOnload,
iframeOnError,
timerHndl,
_url = "",
_responseText = "",
_method,
_timeout = 1000,
_onerror = null,
_onload = null,
_onprogress = null,
_ontimeout = null;
return {
"constructor": function() {
iframe = document.createElement('iframe');
iframe.style.display = "none";
},
"abort": function() {
timerHndl && clearTimeout(timerHndl);
iframeOnload && removeEvent(iframe, "load", iframeOnload);
iframeOnError && removeEvent(iframe, "error", iframeOnError);
iframe.parentNode && iframe.parentNode.removeChild(iframe);
iframeOnload = iframeOnError = timerHndl = null;
},
"open": function(method, url) {
url = (url + "").replace(/#.*$/, "");
_url = url + (url.indexOf("?") >= 0 ? "&" : "?") +
"Access-Control-Allow-Origin=" + window.location.href;
_method = method.toLowerCase() === "post" ? "post" : "get";
iframe.src="javascript:true";
},
"send": function(data) {
data = data || {};
if (typeof data === "string") {
var fields = data.split("&");
data = {};
for(var i = 0; i < fields.length; i++) {
var field = fields[i].split("=");
data[field.shift()] = field.join("=");
}
}
var self = this;
addEvent(iframe, "load", iframeOnload = function() {
var idoc = iframe.contentWindow.document;
removeEvent(iframe, "load", iframeOnload);
idoc.open();
idoc.close();
iframe.contentWindow.name = window.location.href;
addEvent(iframe, "load", iframeOnload = function() {
var
re = /^[\s\S]*?<body><plaintext>(?:>|&gt;)([\s\S]*)$/ig,
error = false;
try {
_responseText = iframe.contentWindow.name;
} catch(__e__) {
error = true;
}
if (!error) {
self.abort.call(self);
if (!re.test(_responseText+"")) {
self.onerror && self.onerror.call(self);
} else {
var specialChars = !/^[\s\S]*?<body><plaintext>>/.test(_responseText);
_responseText = (_responseText+"").
replace(/(?:<\/plaintext><\/body>)?$/i, '').replace(re, '$1');
if (specialChars) {
_responseText = _responseText.
replace(/&amp;/g, "&").replace(/&lt;/g, "<").replace(/&gt;/g, ">");
}
self.onprogress && self.onprogress.call(self);
self.onload && self.onload.call(self);
}
} else {
timerHndl = setTimeout(function() {
self.abort.call(self);
if (_timeout > 1000) {
self.ontimeout && self.ontimeout.call(self);
} else {
self.onerror && self.onerror.call(self);
}
}, _timeout);
}
});
addEvent(iframe, "error", iframeOnError = function() {
self.abort.call(self);
self.onerror && self.onerror.call(self);
});
if (_method === "post") {
var form = idoc.createElement('form');
form.setAttribute("action", _url);
form.setAttribute("method", _method);
form.setAttribute("enctype", "multipart/form-data");
for(var key in data) {
if (data.hasOwnProperty(key)) {
var inp = idoc.createElement('input');
inp.type = "hidden";
inp.name = key;
inp.value = data[key];
form.appendChild(inp);
}
}
idoc.body.appendChild(form);
form.submit();
} else {
iframe.src = _url;
}
});
documentElement.firstChild.appendChild(iframe);
},
"contentType": {
get: function() {
return "";
}
},
"responseText": {
get: function() {
return _responseText;
}
},
"timeout": {
set: function(value) {
_timeout = (value >>> 0) + 1000;
},
get: function() {
return _timeout - 1000;
}
},
"onerror": {
set: function(value) {
_onerror = value;
},
get: function() {
return _onerror;
}
},
"onload": {
set: function(value) {
_onload = value;
},
get: function() {
return _onload;
}
},
"onprogress": {
set: function(value) {
_onprogress = value;
},
get: function() {
return _onprogress;
}
},
"ontimeout": {
set: function(value) {
_ontimeout = value;
},
get: function() {
return _ontimeout;
}
}
}
});
})(window);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment