<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title></title>
</head>
<body>
<div id="test">这是内容</div>
<script language="javascript">
~(function(F){
var moduleCache={};
})((function(){
return window.F={};
})());
F.module=function(url,modDeps,modCallback){
var args=[].slice.call(arguments),
callback=args.pop(),
deps=(args.length&&args[args.length-1] instanceof Array)?args.pop():[],
url=args.length?args.pop():null,
params=[],
depsConut=0,
i=0,
len;
if(len=deps.length){
while(i<len){
(function(i){
depsConut++;
loadModule(deps[i],function(mod){
params[i]=mod;
depsConut--;
if(depsConut===0){
setModule(url,params,callback);
}
});
})(i);
i++;
}
}else{
setModule(url,[],callback);
}
};
var moduleCache={},setModule=function(moduleName,params,callback){},
loadModule=function(moduleName,callback){
var _module;
if(moduleCache[moduleName]){
_module=moduleCache[moduleName];
if(_module.status==='loaded'){
setTimeout(callback(_module.exports),0);
}else{
_module.onload.push(callback);
}
}else{
moduleCache[moduleName]={
moduleName:moduleName,
status:'loading',
exports:null,
onload:[callback]
};
loadScript(getUrl(moduleName));
}
},
getUrl=function(moduleName){
return String(moduleName).replace(/\.js$/g,'')+'.js';
},
loadScript=function(src){
var _script=document.createElement('script');
_script.type='text/javascript';
_script.charset='UTF-8';
_script.async=true;
_script.src=src;
document.getElementsByTagName('head')[0].appendChild(_script);
};
setModule=function(moduleName,params,callback){
var _module,fn;
if(moduleCache[moduleName]){
_module=moduleCache[moduleName];
_module.status='loaded';
_module.exports=callback?callback.apply(_module,params):null;
while(fn=_module.onload.shift()){
fn(_module.exports);
}
}else{
callback&&callback.apply(null,params);
}
};
F.module(['js/com'],function(dom){
dom.html('test','成功');
});
</script>
</body>
</html>
//这里是dom模块的文件在js文件夹内js/dom.js
F.module('js/dom',function(){
return{
g:function(id){
return document.getElementById(id);
},
html:function(id,html){
if(html){
this.g(id).innerHTML=html;
}else{
return this.g(id).innerHTML;
}
},
trim:function(str){
return str.replace(/^\s+|\s+$/g,'');
}
}
});
|