1

JavaScript では、次<script tag>のように iframe にa を挿入しています。

var iframe = document.getElementById('iframe');
var doc= iframe.contentWindow.document.getElementsByTagName('head')[0];

var jquery = document.createElement("script");
jquery.type = "text/javascript";
jquery.src   = "jquery-1.9.1.min.js";

var script   = document.createElement("script");
script.type  = "text/javascript";
script.src   = "blah.js";

doc.appendChild(jquery);
doc.appendChild(script);

ただし、 には jquery コードがありblah.js、jquery スクリプトがまだロードを完了していないために、このスクリプトが失敗することがあります。ロードが完了するまで待つ方法はありますか?

ありがとう。

4

2 に答える 2

3

重要なのはscript.onload = functionName;、IE8 の修正を使用することです。完全なコードについては、https://stackoverflow.com/a/15437678/2227298を参照してください。

jQuery が読み込まれるjQuery.getScriptと、success パラメータを使用できます。

于 2013-05-23T17:30:59.940 に答える
1

最初のスクリプトの最後にコールバックを追加します。

jquery.js の末尾:

if (window.jqueryLoaded)
    window.jqueryLoaded();

あなたのコードで:

// create the function that will be called once jquery finishes loading
function jqueryLoaded()
{
    doc.appendChild(blah);
}

// load jquery
doc.appendChild(jquery);

jquery.js ファイルを変更できない場合は、タイマーを使用する代替 (あまりお勧めしません) の方法:

var checkjQuery = window.setInterval(function() {
    if (!window.jQuery)
        return;
    window.clearInterval(checkjQuery);

    window.jqueryLoaded();
}, 10);
于 2013-05-23T17:13:13.423 に答える