2 * Operaでも非同期リクエストが並列処理できる img-JSONP
3 * http://developer.cybozu.co.jp/takesako/2007/06/opera_img-jsonp.html
5 * iframe を使った jsonp の読み込みエラー判定の記事、
6 * JSONPのエラーを判断する は web archive でも観れない
7 * http://d.hatena.ne.jp/yuushimizu/20090128/1233146321
8 * TODO JSONPの動的取得+エラー処理
9 * http://d.hatena.ne.jp/NeoCat/20110206/1296934235
11 * Safari が JavaScript ファイルを動的ロードできない件
12 * http://www.bricklife.com/weblog/000618.html
14 * IE9でiframe内で遷移した場合window.parentのメソッドを呼べない
15 * http://kozo002.blogspot.jp/2012/07/ie9iframewindowparent.html
17 * IE6(IETester,localhost) で動かない,
23 cb : function( accessKey, jsonString, time, opt_json2FileSize ){
24 if( accessKey !== X_NET_JSONP_ACCESS_KEY || !X_NET_JSONPWrapper._busy ) return;
26 X_NET_JSONPWrapper._busy = false;
30 type : jsonString ? X.Event.SUCCESS : X.Event.ERROR,
31 data : jsonString //window.JSON ? JSON.parse( jsonString ) : eval( jsonString )
34 console.log( 'ms : ' + time + ' speed : ' + ( ( jsonString.length + ( opt_json2FileSize || 0 ) ) / time * 1000 ) + ' バイト/秒.' );
38 var X_NET_JSONP_ACCESS_KEY = Math.random(),
40 X_NET_JSONP_NinjaIframe,
42 X_Net_JSONP_onloadCount;
45 function X_NET_JSONP_loadScriptInNinjaIframe( url ){
46 var json2Path = 'js/libs/json2.js',
47 json2FileSize = 18103,
50 X_NET_JSONP_NinjaIframe || ( X_NET_JSONP_NinjaIframe = new X.Util.NinjaIframe() );
52 // TODO '<scr'+'ipt> 化 恐らくアンチウイルスソフトが反応しないための対策
53 // TODO postMessage の利用
57 ( window[ 'JSON' ] ? '' : '<script src="' + json2Path + '"></script>' ),
59 'onunload=function(){im.onload=im.onerror=""};',
61 'function cb(o){if(nw){nw-=+new Date;parent.X.Net.JSONP.cb(' + X_NET_JSONP_ACCESS_KEY + ',JSON.stringify(o),-nw', window[ 'JSON' ] ? json2FileSize : 0 ,');nw=0}}',
63 '<script id="jp"></script>',
64 '<img id="im" src="', url, '" onload="jp.src=im.src" onerror="jp.src=im.src">'
66 X_Net_JSONP_onloadCount = 2;
70 // JavaScriptでunicode文字列をunescapeする
71 // http://perutago.seesaa.net/article/202801583.html
73 // http://blog.livedoor.jp/dankogai/archives/51503830.html
74 // Ajax - IE8にもJSON入ってます。使えるとは限らないけど
75 // Compatibility mode (別名Quirks mode) では、JSONオブジェクトは無効になります。iframeもだめです
76 '<script id="jp"></script>',
78 'onunload=function(){clearTimeout(id)};',
80 'function cb(o){nw-=+new Date;parent.X.Net.JSONP.cb(' + X_NET_JSONP_ACCESS_KEY + ',parent.JSON.stringify(o).replace(/\\\\u([a-fA-F0-9]{4})/g,function(a,b){return String.fromCharCode(parseInt(b,16))}),-nw)}',
81 'function tm(){jp.src="', url ,'";nw=+new Date}',
82 'id=setTimeout(tm,16);',
85 /* 以下のコードは XP ie8 では動くけど、win8 IE11(8モード)で動かない 開発の便宜を取って,setTimeout を挟む
87 'function cb(o){window.parent.X.Net.JSONP.cb(' + X_NET_JSONP_ACCESS_KEY + ',window.parent.JSON.stringify(o))}',
89 '<script src="', url, '"></script>' */
91 X_Net_JSONP_onloadCount = 2;
95 '<script id="jp"></script>',
97 'onunload=function(){clearTimeout(id)};',
98 'function cb(o){nw-=+new Date;parent.X.Net.JSONP.cb(' + X_NET_JSONP_ACCESS_KEY + ',JSON.stringify(o),-nw)}',
99 'function tm(){jp.src="', url ,'";nw=+new Date}',
100 'id=setTimeout(tm,16);',
103 X_Net_JSONP_onloadCount = 2;
105 if( window[ 'JSON' ] ){
109 'function cb(o){if(nw){nw-=+new Date;parent.X.Net.JSONP.cb(' + X_NET_JSONP_ACCESS_KEY + ',JSON.stringify(o),-nw);nw=0}}',
111 '<script src="', url, '"></script>'
113 X_Net_JSONP_onloadCount = 1;
115 if( X_UA.IE4 || X_UA.MacIE ){
117 '<script id="jn"></script>',
118 '<script id="jp"></script>',
120 'onunload=function(){clearTimeout(id)};',
121 'function cb(o){nw-=new Date;parent.X.Net.JSONP.cb(' + X_NET_JSONP_ACCESS_KEY + ',JSON.stringify(o),-nw-16,', json2FileSize, ')}',
122 'function t1(){document.all.jn.src="', json2Path ,'";id=setTimeout("t2()",16);nw=+new Date}',
123 'id=setTimeout("t1()",16);',
124 'function t2(){if(window.JSON){document.all.jp.src="', url ,'"}else{id=setTimeout("t2()",16)}}',
127 X_Net_JSONP_onloadCount = 3;
129 if( X_UA.IE < 8 ){ // ie5-7
131 '<script id="jn"></script>',
132 '<script id="jp"></script>',
134 'onunload=function(){clearTimeout(id)};',
135 'function cb(o){nw-=new Date;parent.X.Net.JSONP.cb(' + X_NET_JSONP_ACCESS_KEY + ',JSON.stringify(o),-nw-16,', json2FileSize, ')}',
136 'function t1(){jn.src="', json2Path ,'";id=setTimeout(t2,16);nw=+new Date}',
137 'id=setTimeout(t1,16);',
138 'function t2(){if(window.JSON){jp.src="', url ,'"}else{id=setTimeout(t2,16)}}',
141 X_Net_JSONP_onloadCount = 3;
145 'function cb(o){if(nw){nw-=new Date;parent.X.Net.JSONP.cb(' + X_NET_JSONP_ACCESS_KEY + ',JSON.stringify(o),-nw,', json2FileSize, ');nw=0}}',
148 '<script src="', json2Path, '"></script>',
149 '<script src="', url, '"></script>'
151 X_Net_JSONP_onloadCount = 1;
154 X_NET_JSONP_NinjaIframe
155 .refresh( html.join( '' ) )
156 .listen( [ X.Event.SUCCESS, X.Event.ERROR ], X_NET_JSONPWrapper, X_NET_JSONP_iframeListener );
160 function X_NET_JSONP_iframeListener( e ){
162 case X.Event.SUCCESS :
163 console.log( 'iframe onload, but' );
164 if( ++X_NET_JSONPWrapper._onloadCount < X_Net_JSONP_onloadCount ) return;
165 // TODO callback が無ければ error
166 X_NET_JSONPWrapper.asyncDispatch( 1000, X.Event.ERROR );
169 console.log( 'iframe onerror' );
170 X_NET_JSONPWrapper.asyncDispatch( X.Event.ERROR );
173 X_NET_JSONP_NinjaIframe.unlisten();
174 return X.Callback.UN_LISTEN;
178 X_NET_JSONPWrapper = X_Class_override(
179 new X.EventDispatcher(),
186 load : function( url, data, timeout ){
188 X_NET_JSONP_loadScriptInNinjaIframe( url );
194 this._canceled = true;
198 this._busy = this._canceled = false;
199 this._onloadCount = 0;