Skip to content

Instantly share code, notes, and snippets.

@kotarok
Created July 25, 2010 22:27
Show Gist options
  • Save kotarok/489946 to your computer and use it in GitHub Desktop.
Save kotarok/489946 to your computer and use it in GitHub Desktop.
/**
* $.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>');
}
}
})()
@kotarok
Copy link
Author

kotarok commented Oct 27, 2010

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 で同じディレクトリから読み込みます。

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment