私は小さな実験をしました。何かのようなもの:
<html>
<head>
<script src="head.js"></script>
<script src="jquery.min.js"></script>
</head>
<body>
<script type="text/javascript">
jQuery("document").ready(function() {
console.log("ready.document");
});
</script>
...
<script type="text/javascript">
head.js("a.js", "b.js", "c.js", "test.js", function() {
console.log("init.scripts");
var millis = 2000;
var date = new Date();
var curDate = null;
do {curDate = new Date(); }
while(curDate-date < millis);
jQuery("#ddd").text(testTT);
console.log("init.end");
});
</script>
... a lot of html content ...
<div id="ddd"></div>
...
<script type="text/javascript" >
console.log("last line");
</script>
</body>
test.js はコンソールに書き込み、 testTT を定義します。結果は次のとおりです
last line
init.testTT
init.scripts
init.end
ready.document
したがって、この単純なケース (jQuery 1.8 を使用) では、最初にロードされた *.js のコードが実行され、次に init イベントで head.js が生成され、最後に jQuery ready イベントが生成されます。したがって、JQuery が ready イベントを生成するまで待っても問題ありません。ただし、これでも問題が解決しない場合は、次の手順を実行できます。
head.js("a.js", "b.js", "c.js", function() {
jQuery("document").ready(function() {
jQuery("body").trigger("your_event");
});
});
jQuery("document").ready...
*.js startでリッスンする代わりに、トリガーしているカスタム イベントをリッスンしますjQuery("body").on("your_event", handler)
。