Created
July 25, 2010 22:27
-
-
Save kotarok/489946 to your computer and use it in GitHub Desktop.
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
/** | |
* $.require('foo.js'); | |
* $.require(['foo.js','baa.js','moo/muu.js]); | |
*/ | |
jQuery.require= (function(){ | |
var jspath = $('script[src]:last').attr('src'); | |
var dirpath = /.+\//.exec(jspath)[0] | |
return function(jsfiles){ | |
if(typeof(jsfiles) == "array"){ | |
for(var i=0,l=jsfiles.length;i<l;i++){ | |
document.write('<script type="text/javascript" src="' + dirpath + jsfiles[i] + '"><\/script>'); | |
} | |
}else{ | |
document.write('<script type="text/javascript" src="' + dirpath + jsfiles + '"><\/script>'); | |
} | |
} | |
})() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Put above script into external JS file, and load it from HTML, then you can use$.require to load other JS files from the same directory. Once you call this function, you can use $ .require anytime to load JS from the same directory.
上のスクリプトを含んだ外部JSを読み込むと、$.require ってので同じディレクトリ内の他のJSを読み込めます。一回これを読み込んだらいつでもどこでも $.require で同じディレクトリから読み込みます。