我的主要HTML文件动态加载内容;
<html>
<head>
<script src="http://code.jquery.com/jquery-1.7.1.min.js"></script>
</head>
<body>
Loading please wait
<script type="text/javascript">
$(document).ready(function(){
$('body').load("remotePage.html");
});
</script>
</body>
</html>
remotePage.html是;
<html>
<head>
<script src="http://code.jquery.com/jquery-1.7.1.min.js"></script>
<script type="text/javascript" src="script1.js"></script>
<script type="text/javascript" src="script2.js"></script>
<script type="text/javascript" src="script3.js"></script>
</head>
<body>
<script type="text/javascript">
function init(){
someFunctionThatDependsOnAllScripts(); //Fails because all scripts is not yet loaded
}
$(document).ready(init);
<script>
</body>
</html>
失败是因为在加载script1.js之前调用了script1.js中的ready。 在负载上进行回调似乎没有帮助。
$(function(){
$('body').load("remotePage.html", function(){
init(); //doesn't work either
});
});
如何判断所有用于加载remotePage.html所需资源的ajax操作何时完成? 怎么解决?
由于
答案 0 :(得分:0)
将脚本标记更改为:
<script type="text/javascript" src="script1.js" onload="init()"></script>
删除
你脚本中的$(init);
方法。
更新:如果要包含多个脚本,则可以使用以下内容:
<html>
<head>
<script type="text/javascript" src="script1.js" onload="scriptLoaded()"></script>
<script type="text/javascript" src="script2.js" onload="scriptLoaded()"></script>
<script type="text/javascript" src="script3.js" onload="scriptLoaded()"></script>
</head>
<body>
<script type="text/javascript">
//
var totalScriptsToLoad = 3;
function scriptLoaded(){
if(--totalScriptsToLoad == 0){
init();
}
}
//
function init(){
someFunctionFromScript1(); //Fails because script1 is not yet loaded
}
<script>
</body>
</html>