Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
NeoCatさんの「JSONPのエラー検知(&リトライ)可能版。」いいね! http://d.hatena.ne.jp/NeoCat/20110206/1296934235 ※charset=utf-8だけ足した。
// Thanks NeoCat http://d.hatena.ne.jp/NeoCat/20110206/1296934235
window.xds = {
load: function(url, callback, onerror, retry, callback_key) {
var ifr = document.createElement("iframe");
ifr.style.display = "none";
document.body.appendChild(ifr);
var d = ifr.contentWindow.document;
var cnt = 0;
ifr[ifr.readyState/*IE*/ ? "onreadystatechange" : "onload"] = function() {
if (this.readyState && this.readyState != 'complete' || cnt++) return;
if (d.x) {
if (callback) callback.apply(this, d.x);
} else if (retry && retry > 1) {
setTimeout(function(){ xds.load(url, callback, onerror, retry-1) }, 1000);
} else if (onerror)
onerror();
setTimeout(function(){ try { ifr.parentNode.removeChild(ifr); } catch(e) {} }, 0);
};
var url2 = url + (url.indexOf('?')<0?'?':'&') +
(callback_key?callback_key:'callback') + '=cb';
d.write('<scr'+'ipt>function cb(){document.x=arguments}</scr'+'ipt>' +
'<scr'+'ipt charset="utf-8" src="'+url2+'"></scr'+'ipt>');
d.close();
return ifr;
},
abort: function(ifr) {
if (ifr && ifr.parentNode)
ifr.parentNode.removeChild(ifr);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.