1

我在测试 Google Analytics API 时正在加载 javascripts。我的服务器上托管了一些静态 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端。

4

1 回答 1

0

由于同源策略,您不能对第三方域进行 Ajax 调用。通常,您将使用 JavaScript 将 JavaScript 添加到页面

var script = document.createElement("script");
script.src = "foo.js";
document.getElementsByTagName("body")[0].appendChild(script);

这将跨浏览器工作,但您遇到的问题是 Google Analytics 用于document.write在页面上添加新的脚本标签。这意味着它将替换所有页面内容。因此,您无法在页面加载后将其代码动态添加到页面中。

对于异步加载,请参阅 Google 的网站:https ://developers.google.com/analytics/devguides/collection/gajs/asyncTracking

<script type="text/javascript">

  var _gaq = _gaq || [];
  _gaq.push(['_setAccount', 'UA-XXXXX-X']);
  _gaq.push(['_trackPageview']);

  (function() {
    var ga = document.createElement('script'); ga.type = 'text/javascript'; ga.async = true;
    ga.src = ('https:' == document.location.protocol ? 'https://ssl' : 'http://www') + '.google-analytics.com/ga.js';
    var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(ga, s);
  })();

</script>
于 2013-01-08T14:42:48.217 回答