(function(){ var X={}; window.using_xnamespace=using_xnamespace; window.regist_xnamespace=regist_xnamespace; window.check_xnamespace=window.get_xnamespace=check_xnamespace; window.import_xnamespace=import_xnamespace; window.export_xnamespace=export_xnamespace;
var isIE=(window.navigator.appName=="Microsoft Internet Explorer");
function getFilePath(namespace)//跟据命名空间取得文件路径 私有的 { namespace=namespace.split("."); var file_path=X.file_path; var i=0; while((typeof file_path)=="object") { file_path=file_path[namespace[i++]]; } return (file_path); } function check_xnamespace(namespace)//检查命名空间是否存在 如果存在 则得到命名空间 { namespace=namespace.split("."); var xnamespace=X; var i=0; while(i<namespace.length) { if(!xnamespace[namespace[i]])return; xnamespace=xnamespace[namespace[i++]]; } return xnamespace; }
function regist_xnamespace(namespace)//注册命名空间 {
namespace=namespace.split("."); var xnamespace=X; var i=0; while(i<namespace.length) { if(!(xnamespace[namespace[i]]))xnamespace[namespace[i]]={}; //if(typeof xnamespace[namespace[i]]!="object")throw "命名冲突"; xnamespace=xnamespace[namespace[i++]]; } return xnamespace; }
function using_xnamespace(namespace)//检查并加载命名空间 { //alert();
if(check_xnamespace(namespace))return; var file_path=getFilePath(namespace); //alert(file_path);
var scriptElement=document.createElement("script"); scriptElement.src=file_path; if(isIE) { scriptElement.document; } else { //alert(document.getElementsByTagName("head")[0]); scriptElement.onload=function() { X.library_queue.run(); } document.getElementsByTagName("head")[0].appendChild(scriptElement); if(X.library_queue.push(arguments.callee.caller)) throw "库未加载";
} } function import_xnamespace(namespace) { var xnamespace=get_xnamespace(namespace); for(var p in xnamespace) { if(window[p]==xnamespace[p])continue; if(window[p]) throw "命名冲突"; window[p]=xnamespace[p]; } }
function export_xnamespace(namespace) { var xnamespace=get_xnamespace(namespace); for(var p in xnamespace) { if(window[p]==xnamespace[p])window[p]=undefined; } }
X.file_path={ a:{ b:{ c:"./abc.js" }, d:"./ad.js", e:"./ae.js" } } X.library_queue=new Array(); X.library_queue.run=function(){ while(this.length) { (this.pop())(); } } X.library_queue.push=function(f) { for(var i=0;i<this.length;i++) { if(this[i].toString()==f.toString())return false; } this[i]=f; return true; } })();
|