强制顺序下载js文件
最近在做一个东西,界面上用了很多js文件,这些js文件是有先后关系的。而调用这些js文件的方式是使用一个include.js把他们包括进来(因为js文件可能会添加新的),包括方法是使用document.write 方式。这样造成一个问题,页面在载入include.js时,会顺序执行document.write,而文件的大小是不同的,会产生某些靠前的文件在靠后的文件之后被载入,造成运行失败。
因此我想强制按顺序载入这些js文件。初步的思路是第一个文件载入之后,才进行下一个文件的载入。include.js具体代码如下:
//定义要下载的js文件的数组 ,以后只需要修改此数组
var jsNameArray = new Array();
jsNameArray[0] = '/webrct/js/global.js';
jsNameArray[1] ='/webrct/js/pointattr.js';
jsNameArray[2] ='/webrct/js/const.js';
jsNameArray[3] ='/webrct/js/dsbase.js';
jsNameArray[4] ='/webrct/js/rtdata.js';
![](/Images/OutliningIndicators/None.gif)
//求文件名数组长度
var len = jsNameArray.length;
var isOk = new Array(len); //下载完毕标志数组
var isLoading = new Array(len); //正在下载标志数组
var isFinished = false; //全部下载完毕标志
for(var i = 0;i < len;i++)
{
isOk[i] = false;
isLoading[i] = false;
}
![](/Images/OutliningIndicators/None.gif)
//每个文件下载完毕的回调函数
function downOk( n )
{
if (event.srcElement.readyState == 'loaded' || event.srcElement.readyState == 'complete') //确定就绪
{
window.status = '下载完成';
isOk[n] = true;
if (isOk[len -1])
{
isFinished = true;
return;
}else
{
waitDown (n+1);
}
}else
{
waitDown (n);
}
![](/Images/OutliningIndicators/InBlock.gif)
}
![](/Images/OutliningIndicators/None.gif)
//下载文件的具体处理函数
function loadJs(jsName , i)
{
var nextJsName = jsNameArray[i+1];
var s = "<script src='" + jsName + "' language='javascript' onreadystatechange=downOk(" + i + ") charset=GB2312></script>";
var jsFile = document.createElement(s);
![](/Images/OutliningIndicators/InBlock.gif)
document.getElementsByTagName('head').item(0).appendChild(jsFile);
}
![](/Images/OutliningIndicators/None.gif)
//
function waitDown(i)
{
if (!isOk[i]) //还没有下载完成
{
if (!isLoading[i]) //还没有开始下载
{
loadJs(jsNameArray[i] , i);
window.status = '下载js文件:' + jsNameArray[i];
isLoading[i] = true;
}else
{
setTimeout(waitDown, 100, i);
}
}
![](/Images/OutliningIndicators/InBlock.gif)
}
![](/Images/OutliningIndicators/None.gif)
//下载第一个文件
waitDown (0);
因此我想强制按顺序载入这些js文件。初步的思路是第一个文件载入之后,才进行下一个文件的载入。include.js具体代码如下:
![](/Images/OutliningIndicators/None.gif)
![](/Images/OutliningIndicators/None.gif)
![](/Images/OutliningIndicators/None.gif)
![](/Images/OutliningIndicators/None.gif)
![](/Images/OutliningIndicators/None.gif)
![](/Images/OutliningIndicators/None.gif)
![](/Images/OutliningIndicators/None.gif)
![](/Images/OutliningIndicators/None.gif)
![](/Images/OutliningIndicators/None.gif)
![](/Images/OutliningIndicators/None.gif)
![](/Images/OutliningIndicators/None.gif)
![](/Images/OutliningIndicators/None.gif)
![](/Images/OutliningIndicators/None.gif)
![](/Images/OutliningIndicators/None.gif)
![](/Images/OutliningIndicators/None.gif)
![](/Images/OutliningIndicators/None.gif)
![](/Images/OutliningIndicators/ExpandedBlockStart.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/ExpandedBlockEnd.gif)
![](/Images/OutliningIndicators/None.gif)
![](/Images/OutliningIndicators/None.gif)
![](/Images/OutliningIndicators/None.gif)
![](/Images/OutliningIndicators/ExpandedBlockStart.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
![](/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/ExpandedBlockEnd.gif)
![](/Images/OutliningIndicators/None.gif)
![](/Images/OutliningIndicators/None.gif)
![](/Images/OutliningIndicators/None.gif)
![](/Images/OutliningIndicators/ExpandedBlockStart.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/ExpandedBlockEnd.gif)
![](/Images/OutliningIndicators/None.gif)
![](/Images/OutliningIndicators/None.gif)
![](/Images/OutliningIndicators/None.gif)
![](/Images/OutliningIndicators/ExpandedBlockStart.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
![](/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/ExpandedBlockEnd.gif)
![](/Images/OutliningIndicators/None.gif)
![](/Images/OutliningIndicators/None.gif)
![](/Images/OutliningIndicators/None.gif)