Created
September 10, 2013 05:13
-
-
Save tkatochin/6505245 to your computer and use it in GitHub Desktop.
NeoCatさんの「JSONPのエラー検知(&リトライ)可能版。」いいね! http://d.hatena.ne.jp/NeoCat/20110206/1296934235 ※charset=utf-8だけ足した。
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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