2012-03-18 4 views
0

내 메인 HTML 파일이 콘텐츠로 동적으로로드됩니다.JQuery AJAX load() & ready()

<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가 호출되기 때문에 실패합니다. 로드시 콜백이 도움이되지 않습니다.

$(function(){ 
    $('body').load("remotePage.html", function(){ 
    init(); //doesn't work either 
    }); 
}); 

remotePage.html에 필요한 리소스를로드하기위한 모든 아약스 작업이 완료되면 어떻게 알 수 있습니까? 해결 방법?

감사합니다 이것에

+1

먼저 jQuery 라이브러리를 포함 할 필요가 ... –

답변

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> 
+0

을 내가 10 스크립트를 가지고있는 경우는 어떻습니까? – fatbatman

+0

또한 가능한 경우 해당 페이지를 사용할 수 있고 대상을 끝내기 때문에 remotePage.html을 편집하지 않고이 작업을 시도하고 싶습니다. – fatbatman

+0

내 코드를 업데이트했습니다. '3 개의 스크립트'에 대한 답변을 참조하십시오. '10 scripts '도 비슷합니다. – Engineer