也许你可以保留 onload 函数的引用,做你的事情,然后运行原始的 onload 函数。如果您将它放在正文标记内或将其作为脚本包含在生成初始加载的脚本之后,它应该可以按预期工作。
var oldOnLoad = window.onload;
window.onload = function() {
oldOnLoad();
}
编辑为了澄清,您需要oldOnLoad
在页眉和页脚下载后调用。
var oldOnLoad = window.onload;
var headerComplete = false, footerComplete = false;
var callOldOnLoad = function() {
if (headerComplete && footerComplete) {
oldOnLoad();
}
}
window.onload = function() {
$("#header").load("template.html #template_h1", function() {
headerComplete = true;
callOldOnLoad();
});
$("#footer").load("template.html #template_f1", function() {
footerComplete = true;
callOldOnLoad();
});
}
尚未对此进行测试,但您可能会遇到 DOM 在调用之前没有更新的问题oldOnLoad
。所以你可能需要把它放在一个 setTimeout 中。
setTimeout(function() {oldOnLoad();}, 0);