2

このコードを変更することは可能ですか? http://www.myurl.comが実行する必要のあるjsファイルにリダイレクトすると再帰的に機能しますか?

function include(scriptUrl)
{
        var xmlhttp = new XMLHttpRequest();
        xmlhttp.open("GET", scriptUrl);
        xmlhttp.onreadystatechange = function()
        {
            if ((xmlhttp.status == 200) && (xmlhttp.readyState == 4))
            {
                eval(xmlhttp.responseText);
            }
        };
        xmlhttp.send();
}
include("http://www.myurl.com");
4

2 に答える 2

2

なぜそうしないのですか?

$("head").append("<script src='http://myurl.com' type='text/javascript'></script>"); 

もちろん、jqueryなしでそれを行うことはできますが、それがその要点です。GET引数のみを必要とするURL(存在する場合)からプルする場合は、それを含む新しいスクリプト要素を追加する方が簡単(かつ安全)です。

ブラウザはリダイレクトに従うため、これによりリダイレクトの問題も解決されます。

于 2012-08-29T11:11:18.993 に答える
1

When the URL redirects you to another location it will probably give you a 301 status instead of a 200 status. In that case your check should be:

if ((xmlhttp.status == 200 || xmlhttp.status == 301) && xmlhttp.readyState == 4)

Or you can choose to allow all but the error statuses:

if (xmlhttp.status < 400 && xmlhttp.readyState == 4)
于 2012-08-29T11:07:21.370 に答える