Google アナリティクス API のテスト中に JavaScript を遅延読み込みしています。サーバーでホストされているいくつかの静的 JS ファイル (googleAnalyticsAuthorization_v3.js や googleAnalyticsApi_v3.js など) があり、それらを適切にクエリ/評価できますが、次のようにクエリ/評価する方法がわかりません: http://apis. google.com/js/client.js?onload=handleClientLoad
私の AJAX ローダーは簡単です。
function requestJavascriptWithHttpMethod(filePath, httpMethod)
{
request = new XMLHttpRequest();
request.open(httpMethod, filePath, true);
request.onreadystatechange = function()
{
_log('Request "'+filePath+'": readyState <'+requestReadyStateString(this.readyState)+'>, status <'+requestStatusString(this.status)+'>.');
if (this.readyState == this.DONE &&
requestStatusString(this.status) == 'OK')
{
_log('Loading of "'+filePath+'" finished.');
eval(this.responseText);
}
}
request.send();
_log('Request "'+filePath+'"...');
}
function requestJavascript(filePath)
{ requestJavascriptWithHttpMethod(filePath, "GET"); }
URL を投稿しようとしましたが、結果が実行されませんでした (実際にはステータス コード 0 で返されます)。
requestJavascript('googleAnalyticsAuthorization_v3.js'); //Loads, evaluates well.
requestJavascript('googleAnalyticsApi_v3.js'); //Loads, evaluates well.
requestJavascriptWithHttpMethod('http://apis.google.com/js/client.js?onload=handleClientLoad', "POST"); //Nothing seems happening, returns with status code 0.
次のように、クライアントの HTML コードに単純に含めたのと同じように動作する必要があります。
<script src="http://apis.google.com/js/client.js?onload=handleClientLoad"></script>
コンソール出力は次のとおりです。
Request "googleAnalyticsAuthorization_v3.js"...
Request "googleAnalyticsApi_v3.js"...
Request "http://apis.google.com/js/client.js?onload=handleClientLoad"...
Request "http://apis.google.com/js/client.js?onload=handleClientLoad": readyState <request finished and response is ready>, status <0>.
Request "googleAnalyticsApi_v3.js": readyState <request received>, status <OK>.
Request "googleAnalyticsApi_v3.js": readyState <processing request>, status <OK>.
Request "googleAnalyticsApi_v3.js": readyState <request finished and response is ready>, status <OK>.
Loading of "googleAnalyticsApi_v3.js" finished.
googleAnalytics_v3.js evaluated
Request "googleAnalyticsAuthorization_v3.js": readyState <request received>, status <OK>.
Request "googleAnalyticsAuthorization_v3.js": readyState <processing request>, status <OK>.
Request "googleAnalyticsAuthorization_v3.js": readyState <request finished and response is ready>, status <OK>.
Loading of "googleAnalyticsAuthorization_v3.js" finished.
googleAnalyticsAuthorization_v3.js evaluated
ここで誰か助けてくれませんか?
実際には、すべてのロジックを JavaScript にカプセル化し、HTML 側に依存しないようにしたいだけです。