ææ°çã¯ãã¡ã â JavaScriptで外部ライブラリを読み込むためのスクリプトをCodeRepos.orgに上げた。 - ヒルズで働く@robarioの技ログ
æ¹è¯ã«ä¼´ããã¨ã³ããªã¼ãå ¨é¢çã«æ¸ãæãã¾ããã
- å¾ããåå©ç¨ã§ããããã«ååãä»ãã¾ããï¼ScriptRunnerï¼
- å¤é¨JavaScriptãèªã¿è¾¼ãã¿ã¤ãã³ã°ãæåã«æã£ã¦ãã¾ãããä»ã¾ã§å¤é¨JavaScriptãèªã¿è¾¼ãã ãã®å ´åã§ãScriptRunner({...}) ();ã¨æ¸ããªãã¨ãããªãã£ãã®ã§ããScriptRunner({...});ã¨æ¸ããããã«ãªãã¾ããã
- å¤é¨JavaScriptã®èªã¿è¾¼ã¿å¾ãarguments.calleeãè¿ãããã«ãã¾ãããããã«ãããScriptRunner({...}) () () () () () () ;ã¨æ¸ãã¦ãåãããã«ãªãã¾ããã
- å¤é¨JavaScriptã®èªã¿è¾¼ã¿å®äºãå¾ ã¤ãã©ããæå®ã§ããããã«ãªãã¾ããã
- IEã®å ´åãscriptè¦ç´ ã追å ãã¦ãã³ã³ããã¹ããwindowãããªãæï¼ä½ã¦è¨ãã°ããã®ããããããè¦ããã«this!=windowã®æï¼ãèªã¿è¾¼ã¿èªä½ã¯è¡ãã®ã§ãããè©ä¾¡ã¯ãã¦ãããªããããªã®ã§setTimeoutã§éãããã¨ã«ãã¦ã¿ã¾ããã
- ãw=window;t=setTimeoutãã¨ãªã£ã¦ãã¦tãã°ãã¼ãã«ã«çªã£è¾¼ãã§ããã®ããw=window,t=setTimeoutãã«ä¿®æ£ãnãã°ãã¼ãã«ã«çªã£è¾¼ãã§ããã®ãä¿®æ£ã
çµæ§ä½¿ãåæãè¯ããªã£ã¦ãã¾ããã
å
ã®ã¹ã¯ãªãããä¸åå¤æ´ããç´åã«ã³ã¼ããæ¿å
¥ããã ãã§è¯ãã®ããã¤ã³ãã§ãã
HTMLå´ã«æãå
¥ããå¿
è¦ããªããããå¤é¨JavaScriptãèªã¿è¾¼ãã¿ã¤ãã®ããã¯ãã¼ã¯ã¬ããã§ã¯ãèªã¿è¾¼ã¾ããJavaScriptã®å
é¨ããæ´ã«ä»ã®å¤é¨JavaScriptãèªã¿è¾¼ããã¨ãã§ãã¾ãã
Sjaxを使わないJavaScript Loader - ヒルズで働く@robarioの技ログããæ´¾çãã¦ãã¾ãã
âJavaScriptãæ¸ãã¦ã¿ã
âå¤é¨JavaScriptãèªã¿è¾¼ã¿ãããªã¼ã
âdocument.write('<script...')ãããç»é¢ãçã£ç½Î£ã½(ï¾Ðï¾; )ï¾
âdocument.appendChild(script)ã§OKï¼+。:.ï¾ã½(*´â`)ï¾ï¾.:。+ï¾
âå¤é¨JavaScriptã®èªã¿è¾¼ã¿ãçµãã£ã¦ãªãã®ã«å¦çãé²ããããã¼ï¼ï¼ï¼ï¼
âãã¼ãã3ç§éå¾
ã£ã¦ãão(ï½Ï´*)o
âsetTimeout(...,3000) ä½ãæ ¼å¥½æªã(´ã»Ïã»ï½)
ãããªæ¹ã«æ§ããã¨ã³ããªã¼ã
å¤é¨JavaScriptã®èªã¿è¾¼ã¿æ¹
JavaScriptå ããå¤é¨JavaScriptãèªã¿è¾¼ãå ´åã<script>ã¿ã°ãdocument.writeããæ¹æ³ã»scriptè¦ç´ ãDOMã§è¿½å ããæ¹æ³ã»Ajaxã§èªã¿è¾¼ãæ¹æ³ãããã¾ããããããã¡ãªããã»ãã¡ãªãããããã¾ãã
<script>ã¿ã°ãdocument.writeãã
- documentãcloseããã¦ããªããã¨ãå¿ è¦
window.onloadå ãBookmarkletã§ã¯æ¢ã«documentãcloseããã¦ãã¾ã£ã¦ããã®ã§ãã®æ¹æ³ã¯ä½¿ãã¾ããã
- èªã¿è¾¼ã¿ãå®äºããã¾ã§åæ¢ãã
å¤é¨JavaScriptã®èªã¿è¾¼ã¿ãå®äºãã¦ãã次ã®è¡ã¸è¡ããããå¾ç¶ããJavaScriptã¯èªã¿è¾¼ã¿ãå®äºãã¦ãããã¨ãåæã«æ¸ããã¨ãã§ãã¾ãã
ãã ããã©ã¦ã¶ã®å¦çãåæ¢ãã¦ãã¾ããããå¤é¨JavaScriptã®èªã¿è¾¼ã¿ã«æéããããã¨ãã¼ã¸ã¬ã³ããªã³ã°ãããã§æ¢ã¾ã£ã¦ãã¾ãã¾ãã
scriptè¦ç´ ãDOMã§è¿½å ãã
- ãã¤ã§ã使ãã
- èªã¿è¾¼ã¿ä¸ããã©ã¦ã¶ã®å¦çã¯æ¢ã¾ããªã
ãã ãèªã¿è¾¼ã¿ããã¤å®äºãããåããã¾ããã
Ajaxã§èªã¿è¾¼ã
- å¤é¨ãã¡ã¤ã³ã®JavaScriptãèªã¿è¾¼ããªã
çãã§ãã
- Sjaxã§èªã¿è¾¼ãã§ããéããã©ã¦ã¶ã®å¦çãåæ¢ãã
<script>ã¿ã°ãdocument.writeããå ´åã¨åãã§ãã
ä»åã®è©±
scriptè¦ç´ ãDOMã§è¿½å ããwindowãªãã¸ã§ã¯ãã®å¤åã調ã¹ã¦èªã¿è¾¼ã¾ãããã©ãããå¤æããæ¹æ³ãããããã¨ããã®ãååã®è©±ã
ä»åã¯ãæ¢åã®JavaScriptã«ç°¡åã«ä»ãããå¤ãããã§ããããã«ãã¾ããããã¨ãã話ã§ãã
æ¡ä»¶
ã¾ã
- JavaScriptã (function(){/*...*/})() ã¨ããå½¢ã«ãªã£ã¦ãã
- å¤é¨ã¹ã¯ãªãããwindowãªãã¸ã§ã¯ãã«ããããã£ã追å ãã
ã¨ããæ¡ä»¶ãæºããå¿ è¦ãããã¾ãã1çªç®ã¯æ§æçãªå¶éã§ã2çªç®ã¯ä»çµã¿çãªå¶éã§ãã
1çªç®ã®æ¡ä»¶ãæºãããªãå ´åã大æµã¯ã(function(){ãã¨ã})()ãã§å²ãã°ä¸æãããã¾ãã
追å ããã³ã¼ã
æ¢åã®JavaScriptã®ç´åã«ãããªã³ã¼ããæ¿å ¥ãã¾ãã
(ScriptRunner = function(libs) { var waiting = {}; for (var i = 0, n = libs.length; i < n; ++i) { if (typeof libs[i] == 'string' || libs[i] instanceof String) { libs[i] = {'': libs[i]}; } for (var prop in libs[i]) { if (prop) { if (window[prop]) { continue; } waiting[prop] = 1; } (function(src){ var script = document.createElement('script'); script.type = 'text/javascript'; script.charset = 'UTF-8'; script.src = src; setTimeout(function(){document.documentElement.appendChild(script)}, 0); })(libs[i][prop]); } } return function(func) { if (func) { setTimeout(function() { for (var prop in waiting){ if (!window[prop]) { return setTimeout(arguments.callee, 99); } } func(); }, 0); } return arguments.callee; } }) ([ // ããã§èªã¿è¾¼ã¿ããã©ã¤ãã©ãªãæå® ]) // âããããæ¢åã®JavaScript
çãã¾ã¨ããã¨ãããªãã¾ããã©ã¡ãã使ã£ã¦ãæ§ãã¾ããã
(ScriptRunner=function(v){var i,n,l,p,c={},w=window,t=setTimeout;for(i=0,n=v.length;i<n;++i){l=v[i];if(typeof l=='string'||l instanceof String){l={'':l}}for(p in l){if(p){if(w[p]){continue}c[p]=1}(function(j){var s=document.createElement('script');s.type='text/javascript';s.charset='UTF-8';s.src=j;t(function(){document.documentElement.appendChild(s)},0)})(l[p])}}return function(f){if(f){t(function(){for(p in c){if(!w[p]){return t(arguments.callee,99)}}f()},0)}return arguments.callee}}) ([ // ããã§èªã¿è¾¼ã¿ããã©ã¤ãã©ãªãæå® ]) // âããããæ¢åã®JavaScript
functionã2段éã«åãã¦è¿ãã¦ããã®ã«ã¯æå³ãããã¾ãã
ä»åã®ãããªãã¨ãããã«ã¯ãæ¢åã®JavaScriptãé¢æ°ãªãã¸ã§ã¯ãã¨ãã¦åãåãå¿
è¦ãããã¾ãã
ã§ããåç´ã«ãåãåã£ãé¢æ°ããå®è¡ããé¢æ°ãã使ãã¨
function foo(f) { f(); } foo(function(){ // âããã¾ã§è¿½å (function(){...})() }); // ããã«ãä¿®æ£ãå¿ è¦ï¼
ã®ããã«å
¨ä½ãå²ãå¿
è¦ãããã¾ããæ«å°¾ã®ä¿®æ£ãå«ãªæãã§ãã
ãããç¡ããã«ã¯å¤ªåã®(,)ããé¢æ°ã®å¼æ°ã示ãæ¬å¼§ãã¨ã¿ãªãã¦ããããåãåã£ãé¢æ°ããå®è¡ããé¢æ°ããè¿ãé¢æ°ããåç½®ããã°è¯ãã®ã§ãã
function foo(f) { return function() { f(); } } foo // âããã¾ã§è¿½å âããããä¿®æ£ä¸è¦ (function(){...})()
ãã®ããã«ãã¦ããã°ã追å ã³ã¼ãé¨åãã°ã£ããå¤é¨JavaScriptã®å
容ã«ç½®ãæããï¼éçã«å±éããï¼ã®ã楽ã§ãã
ã¨ããããå
ã
ã®ç®çã¯ããã ã£ãããã¾ããéçºä¸ã¯å¤é¨JavaScriptãåçã«èªã¿è¾¼ã¿ããªãªã¼ã¹æã¯ä¸ã¤ã®JavaScriptãã¡ã¤ã«ã«ã¾ã¨ãããã¨ãã£ããã¨ãPerlã§ç°¡åã«æ¸ãã¾ãã
ä¾
ä¾ãã°ãããªã¹ã¯ãªãããããã¨ã
(function(){ alert(typeof JSONScriptRequest); })();
ãã®ã¾ã¾å®è¡ããã¨å½ç¶ã§ãã"undefined"ãåºåããã¾ããããã§ç´åã«ä¸è¨ã³ã¼ããæ¿å ¥ãã¾ãã
(ScriptRunner=function(v){var i,n,l,p,s,c={},w=window,t=setTimeout;for(i=0,n=v.length;i<n;++i){l=v[i];if(typeof l=='string'||l instanceof String){l={'':l}}for(p in l){if(p){if(w[p]){continue}c[p]=1}s=document.createElement('script');s.type='text/javascript';s.charset='UTF-8';s.src=l[p];document.documentElement.appendChild(s)}}return function(f){if(f){t(function(){for(p in c){if(!w[p]){return t(arguments.callee,99)}}f()},0)}return arguments.callee}}) ([ {JSONScriptRequest: 'http://www.openjsan.org/src/y/yo/yoshida/JSONScriptRequest-0.02/lib/JSONScriptRequest.js'} ]) (function(){ alert(typeof JSONScriptRequest); })();
ãããå®è¡ããã¨ãJSONScriptRequest.jsã®èªã¿è¾¼ã¿ãå®äºãã¦ããalertãå®è¡ãããã®ã§ãå¿ ã"function"ãåºåããã¾ãã
å¤é¨JavaScriptã®èªã¿è¾¼ã¿å®äºãå¾ ã¤å¿ è¦ããªãå ´åãobjectã®å¤ããã«stringã渡ãã¾ãã
(ScriptRunner=function(v){var i,n,l,p,s,c={},w=window,t=setTimeout;for(i=0,n=v.length;i<n;++i){l=v[i];if(typeof l=='string'||l instanceof String){l={'':l}}for(p in l){if(p){if(w[p]){continue}c[p]=1}s=document.createElement('script');s.type='text/javascript';s.charset='UTF-8';s.src=l[p];document.documentElement.appendChild(s)}}return function(f){if(f){t(function(){for(p in c){if(!w[p]){return t(arguments.callee,99)}}f()},0)}return arguments.callee}}) ([ 'http://www.openjsan.org/src/y/yo/yoshida/JSONScriptRequest-0.02/lib/JSONScriptRequest.js' ]) (function(){ alert(typeof JSONScriptRequest); })();
ãã ããã®ä¾ã§ã¯"undefined"ãåºåããã¾ããJSONScriptRequest.jsã®èªã¿è¾¼ã¿ãå®äºããåã«alertãå®è¡ããã¦ãã¾ãããã§ãã