Fresh stuff I wrote all afternoon this afternoon
boot.js is a core file
compressed package
test.html references
all the js files of ad.js, and there is such a relationship:
ad.js->ae.js
ad.js->abc.js
ae.js->abc.js
xnamespace implements synchronous loading of js files. As long as the js file is written according to the specification, using_xnamespace can be used to ensure that the libraries it depends on are loaded before it.
The only requirement xnamespace has for js files is that the file must be enclosed with an unnamed function closure (function(){ })(). Even if xnamespace is not used, this is also the practice of many high-quality codes. The tested browser ie6 ie7 ff NetScape Safari
The following is the content of boot.js
for the browser opera swift that failed the test
[Copy]Code:
(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)//Get the file path privately based on the 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)//Check whether the namespace exists, if it exists, get the 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)//Register 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 "Naming conflict";
xnamespace=xnamespace[namespace[i++]];
}
return xnamespace;
}
function using_xnamespace(namespace)//Check and load the 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 "Library not loaded";
}
}
function import_xnamespace(namespace)
{
var xnamespace=get_xnamespace(namespace);
for(var p in xnamespace)
{
if(window[p]==xnamespace[p])continue;
if(window[p]) throw "Naming conflict";
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;
}
})();
10.21 2007 quietly updated: separated the Exception and FilePath files