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 //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;
43 function X_NET_JSONP_loadScriptInNinjaIframe( url ){
44 var json2Path = 'js/libs/json2.js',
45 json2FileSize = 18103,
48 X_NET_JSONP_NinjaIframe || ( X_NET_JSONP_NinjaIframe = new X.Util.NinjaIframe() );
50 // TODO '<scr'+'ipt> 化 恐らくアンチウイルスソフトが反応しないための対策
54 // http://blog.livedoor.jp/dankogai/archives/51503830.html
55 // Ajax - IE8にもJSON入ってます。使えるとは限らないけど
56 // Compatibility mode (別名Quirks mode) では、JSONオブジェクトは無効になります。iframeもだめです
57 /* 以下のコードは XP ie8 では動くけど、win8 IE11(8モード)で動かない 開発の便宜を取って,setTimeout を挟む
59 'function cb(o){window.parent.X.Net.JSONP.cb(' + X_NET_JSONP_ACCESS_KEY + ',window.parent.JSON.stringify(o))}',
61 '<script src="', url, '"></script>' */
62 '<script id="jp"></script>',
65 'function cb(o){nw-=+new Date;parent.X.Net.JSONP.cb(' + X_NET_JSONP_ACCESS_KEY + ',parent.JSON.stringify(o),-nw)}',
66 'function tm(){jp.src="', url ,'";nw=+new Date}',
73 '<script id="jp"></script>',
75 'function cb(o){nw-=+new Date;parent.X.Net.JSONP.cb(' + X_NET_JSONP_ACCESS_KEY + ',JSON.stringify(o),-nw)}',
76 'function tm(){jp.src="', url ,'";nw=+new Date}',
81 if( window[ 'JSON' ] ){
85 'function cb(o){if(nw){nw-=+new Date;parent.X.Net.JSONP.cb(' + X_NET_JSONP_ACCESS_KEY + ',JSON.stringify(o),-nw);nw=0}}',
87 '<script src="', url, '"></script>'
90 if( X_UA.IE4 || X_UA.MacIE ){
92 '<script id="jn"></script>',
93 '<script id="jp"></script>',
95 'function cb(o){nw-=new Date;parent.X.Net.JSONP.cb(' + X_NET_JSONP_ACCESS_KEY + ',JSON.stringify(o),-nw-16,', json2FileSize, ')}',
96 'function t1(){document.all.jn.src="', json2Path ,'";setTimeout("t2()",16);nw=+new Date}',
97 'setTimeout("t1()",16);',
98 'function t2(){if(window.JSON){document.all.jp.src="', url ,'"}else{setTimeout("t2()",16)}}',
102 if( X_UA.IE < 8 ){ // ie5-7
104 '<script id="jn"></script>',
105 '<script id="jp"></script>',
107 'function cb(o){nw-=new Date;parent.X.Net.JSONP.cb(' + X_NET_JSONP_ACCESS_KEY + ',JSON.stringify(o),-nw-16,', json2FileSize, ')}',
108 'function t1(){jn.src="', json2Path ,'";setTimeout(t2,16);nw=+new Date}',
109 'setTimeout(t1,16);',
110 'function t2(){if(window.JSON){jp.src="', url ,'"}else{setTimeout(t2,16)}}',
116 '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}}',
119 '<script src="', json2Path, '"></script>',
120 '<script src="', url, '"></script>'
124 X_NET_JSONP_NinjaIframe
125 .refresh( html.join( '' ) )
126 .listen( [ X.Event.SUCCESS, X.Event.ERROR ], X_NET_JSONPWrapper, X_NET_JSONP_iframeListener );
130 function X_NET_JSONP_iframeListener( e ){
132 case X.Event.SUCCESS :
133 console.log( 'iframe onload' );
137 console.log( 'iframe onerror' );
138 X_NET_JSONPWrapper.asyncDispatch( 0, X.Event.ERROR );
144 X_NET_JSONPWrapper = X_Class_override(
145 new X.EventDispatcher(),
148 _operaImage : null, // X.Net.Image();
152 load : function( url, data, timeout ){
154 if( X_NET_JSONP_operaImageHandleEvent ){
155 this._operaImage = X.Net.Image( url, false )
156 .listenOnce( [ X.Event.SUCCESS, X.Event.ERROR, X.Event.TIMEOUT ], X_NET_JSONP_operaImageHandleEvent );
158 X_NET_JSONP_loadScriptInNinjaIframe( url );
165 if( this._operaImage ){
167 .unlisten( [ X.Event.SUCCESS, X.Event.ERROR, X.Event.TIMEOUT ], X_NET_JSONP_operaImageHandleEvent )
169 delete this._operaImage;
171 this._canceled = true;
176 this._busy = this._canceled = false;
181 function X_NET_JSONP_operaImageHandleEvent( e ){
183 case X.Event.SUCCESS :
185 X_NET_JSONP_loadScriptInNinjaIframe( e.src );
187 case X.Event.TIMEOUT :
188 X_NET_JSONPWrapper._operaImage
189 .unlisten( [ X.Event.SUCCESS, X.Event.ERROR, X.Event.TIMEOUT ], X_NET_JSONP_operaImageHandleEvent )
191 X_NET_JSONPWrapper.asyncDispatch( 0, X.Event.ERROR );
195 if( !X_UA.Opera ) X_NET_JSONP_operaImageHandleEvent = null;