0

私のメイン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操作がいつ終了したかを知るにはどうすればよいですか?直し方?

ありがとう

4

1 に答える 1

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>
于 2012-03-18T15:08:15.733 に答える