2

重要なのは、ブックマークレットに、現在のWebページにjquery、jquery-ui、およびcssファイルを作成する外部jsをロードさせ、すべてをクロスドメインで実行することです。何らかの理由で、jqueryコードが実行されるまでに、jquery-uiに含まれているdialog()関数が存在しません。私の推測では、jqueryは外部スクリプトがロードされる前にコードを実行しています。

助けてください!

 function loadjscssfile(filename, filetype){
 if (filetype=="js"){ //if filename is a external JavaScript file
  var fileref=document.createElement('script')
  fileref.setAttribute("type","text/javascript")
  fileref.setAttribute("src", filename)
 }
 else if (filetype=="css"){ //if filename is an external CSS file
  var fileref=document.createElement("link")
  fileref.setAttribute("rel", "stylesheet")
  fileref.setAttribute("type", "text/css")
  fileref.setAttribute("href", filename)
 }
 if (typeof fileref!="undefined")
  document.getElementsByTagName("head")[0].appendChild(fileref)
}

loadjscssfile("http://localhost/js/jquery-1.3.2.min.js", "js") //dynamically load and add this .js file
loadjscssfile("http://localhost/js/jquery-ui-1.7.2.custom.min.js", "js") //dynamically load "javascript.php" as a JavaScript file
loadjscssfile("http://localhost/css/redmond/jquery-ui-1.7.2.custom.css", "css") //dynamically load and add this .css file

$(document).ready(function() {
    var title = document.title;
    var location = document.location;
    var documentInfo = "url="+location+"&title="+title;


    div = $("<div id=dialog name=dialog>").html("<img src='http://localhost/ajax-loader.gif' alt='loader' style='padding-left:15px;'/>");

    $("body").prepend(div);
    $("#dialog").dialog();


    var script = $.ajax({url: "http://localhost/parse.php", data: documentInfo, async: false}).responseText;
    $("#dialog").html(script);


});
4

2 に答える 2

2

で$関数を呼び出しているときにスクリプトがクラッシュしないのはすでに幸運です$(document):)

これが例です。

test.js:

function sleep(milliseconds)
{
    var end = new Date().getTime() + milliseconds;

    while (new Date().getTime() < end);

    document.write('external script executed<br/>');
}

sleep(3000);

test.html

<html>
<head>
    <title>test</title>
    <script type="text/javascript">
        var fileref=document.createElement('script');
        fileref.setAttribute("type","text/javascript");
        fileref.setAttribute("src", "test.js");
        document.getElementsByTagName("head")[0].appendChild(fileref)

        document.write('page script executed<br/>');
    </script>
</head>
<body>
</body>
</html>

test.htmltest.jsドキュメントに書き込む前にロードします。結果が次のようになると予想する場合:

実行された外部スクリプト

実行されたページスクリプト

...それならあなたは間違っています。

ページのに<script>要素を追加すると、要素が追加された直後に関数が戻り、スクリプトが完全に読み込まれるのを待ちません。上記の例では、出力は次のようになります。<head>appendChild()

実行されたページスクリプト

実行された外部スクリプト

これが、完全にロードされているかどうかわからないときにjQueryを使用できるのは幸運だと言った理由です。

この問題の回避策は、ドキュメント$(document).ready(...);のにスクリプトとしてを追加することです。<head>通常のブラウザ(つまり、私が言っていることを理解している場合)は、スクリプトをページに追加した順序でロードして実行することが期待できます。

    var pageScript = document.createElement('script');
    pageScript.setAttribute("type", "text/javascript");
    var pageScriptSource = document.createTextNode("document.write('page script executed<br/>');");
    pageScript.appendChild(pageScriptSource);
    document.getElementsByTagName("head")[0].appendChild(pageScript);

FFおよびIEでテスト済み。FFで動作します...

これは完全な解決策ではありませんが、IEのハックを見つける時間がありません(そして望んでいません);)

于 2010-01-05T12:47:18.320 に答える
0

jQuery.getScriptを使用してみてください:http://docs.jquery.com/Ajax/jQuery.getScript

スクリプトのロード時に実行されるコールバックを指定できます。

于 2010-01-05T11:02:01.233 に答える