我有一个网页,我按顺序加载脚本,因为每个加载的脚本都需要在它之前加载脚本。例如,js/global.js
,js/menu.js
,js/loan.js
和js/contribution.js
按顺序需要彼此,contributions.js
需要loan.js
,loan,js
需要menu.js
,等等。我已设法通过以下代码实现此目的。
$(document).ready(function () {
LoadScriptscontri();
});
function LoadScriptscontri(async) {
if (async === undefined) {
async = false;
}
var scripts = [];
var _scripts = [ 'js/global.js', 'js/menu.js', 'js/loan.js', 'js/contributions.js'];
if (async) {
LoadScriptsAsync(_scripts, scripts)
} else {
LoadScriptsSync(_scripts, scripts)
}
}
// what you are looking for :
function LoadScriptsSync(_scripts, scripts) {
var x = 0;
var loopArray = function (_scripts, scripts) {
// call itself
loadScript(_scripts[x], scripts[x], function () {
// set x to next item
x++;
// any more items in array?
if (x < _scripts.length) {
loopArray(_scripts, scripts);
}
});
}
loopArray(_scripts, scripts);
}
// async load as in your code
function LoadScriptsAsync(_scripts, scripts) {
for (var i = 0; i < _scripts.length; i++) {
loadScript(_scripts[i], scripts[i], function () { });
}
}
// load script function with callback to handle synchronicity
function loadScript(src, script, callback) {
script = document.createElement('script');
script.onerror = function () {
// handling error when loading script
alert('Error to handle')
}
script.onload = function () {
console.log(src + ' loaded ')
callback();
}
script.src = src;
document.getElementsByTagName('head')[0].appendChild(script);
}
但问题是我何时使用window.location = "http://cooperative-izumedia.rhcloud.com/contributions.html";
将页面更改为window.location = "http://cooperative-izumedia.rhcloud.com/dashboard.html";
,然后按所需顺序加载,而不是首先加载contributions.js
。究竟发生了什么以及如何摆脱这种情况。我在firefox和chrome中测试了它,我仍然得到相同的结果。
我的调试信息
dashboardjsloader.js:54 js / global.js已加载
dashboardjsloader.js:54 js / menu.js已加载
dashboardjsloader.js:54 js / loan.js已加载
dashboardjsloader.js:54 js / dashboard.js已加载
menu.js:菜单中的113个id.js = d2b2aecb0197b6f439056
/*here am spitting out the id got from menu.js*/
dashboard.js:119 0
/*here dashboard.js is able to determine that the section it will work on is of index 0 thanks to menu.js*/
导航至http://cooperative-izumedia.rhcloud.com/contributions.html
/*here I change page*/
contributionsjsloader.js:54 js / global.js已加载
contributionsjsloader.js:54 js / menu.js已加载
contributionsjsloader.js:54 js / loan.js已加载
contributions.js:71 Contri something
/*For some reason menu.js isnt't called to spit out its value before contribution.js tell us something even before contibution.js is loaded which loaded below*/
contributionsjsloader.js:54 js / contributions.js已加载
menu.js:183无法生成索引TypeError:无法读取属性&#39; removeAttribute&#39;未定义的
/*Now menu.js decides to show its face and this exception is allowed, its expected.*/
menu.js:菜单中的113个id.js = d2b2aecb0197b6f439056
/* and then it finally spits out its value*/
导航至http://cooperative-izumedia.rhcloud.com/dashboard.html
dashboardjsloader.js:54 js / global.js已加载
dashboardjsloader.js:54 js / menu.js已加载
dashboardjsloader.js:54 js / loan.js已加载
dashboardjsloader.js:54 js / dashboard.js已加载
/* I go back to dashboard and because menu is not called first my dashboard.js goes to complete shits */
dashboard.js:119 NaN
dashboard.js:132 TypeError:无法设置属性&#39; innerHTML&#39;未定义的
仪表板中的dashboard.js:75 TypeError:无法设置属性&#39; innerHTML&#39;的 未定义
/*Now menu.js rears its ugly head*/
menu.js:菜单中的113个id.js = d2b2aecb0197b6f439056