使用url字符串加载Javascript AJAX?

使用url字符串加载Javascript AJAX?,javascript,ajax,post,google-analytics-api,Javascript,Ajax,Post,Google Analytics Api,我在测试Google Analytics API时加载Java脚本。 我的服务器上有一些静态JS文件(如googleAnalyticsAuthorization_v3.JS和googleAnalyticsApi_v3.JS),我可以很好地查询/评估它们,但我不知道如何查询/评估以下内容: 我的AJAX加载程序非常简单: function requestJavascriptWithHttpMethod(filePath, httpMethod) { request = new XMLHt

我在测试Google Analytics API时加载Java脚本。 我的服务器上有一些静态JS文件(如googleAnalyticsAuthorization_v3.JS和googleAnalyticsApi_v3.JS),我可以很好地查询/评估它们,但我不知道如何查询/评估以下内容:

我的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"); }   
它的行为应该与我只是将其包含在客户端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 
请求“googleAnalyticsAuthorization_v3.js”。。。
请求“googleAnalyticsApi_v3.js”。。。
请求“http://apis.google.com/js/client.js?onload=handleClientLoad"...
请求“http://apis.google.com/js/client.js?onload=handleClientLoad“:readyState,状态。
请求“googleAnalyticsApi_v3.js”:readyState,status。
请求“googleAnalyticsApi_v3.js”:readyState,status。
请求“googleAnalyticsApi_v3.js”:readyState,status。
“googleAnalyticsApi_v3.js”加载完成。
googleAnalytics_v3.js已评估
请求“googleAnalyticsAuthorization_v3.js”:readyState,status。
请求“googleAnalyticsAuthorization_v3.js”:readyState,status。
请求“googleAnalyticsAuthorization_v3.js”:readyState,status。
“googleAnalyticsAuthorization_v3.js”加载完成。
googleAnalyticsAuthorization_v3.js已评估
有人能帮我吗


实际上,我只想将所有逻辑封装到javascript中,不依赖于HTML端。

由于同源策略,您无法对第三方域进行Ajax调用。通常,您将使用

var script = document.createElement("script");
script.src = "foo.js";
document.getElementsByTagName("body")[0].appendChild(script);
这可以跨浏览器使用,但问题是Google Analytics使用
document.write
在页面上添加新的脚本标记。这意味着它将替换所有页面内容。因此,您无法在页面加载后将其代码动态添加到页面

有关异步加载,请参见谷歌网站:


var _gaq=_gaq | |[];
_gaq推送(['''u setAccount','UA-XXXXX-X']);
_gaq.push([''u trackPageview']);
(功能(){
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);
})();

根据您的代码,您使用的http方法是GET而不是POST,不是吗?我通过调用requestJavascriptWithHttpMethod(“…”,“POST”)来运行实际请求;您看到的“GET”只是一个快捷功能。我先尝试了这个方法,但没有成功。替换所有页面内容是什么意思?它不是,我的意思是,当我是jsut的时候,我的工作就是将这些文件包含在HTML端的标记中。但是我更喜欢在HTML中“隐藏”GA细节。啊,你是说(?):r.write(“”)-是的,这就是document.write行。我只是想隐藏。。。来自代码行,以便于simplyer重用。只是把它放在别处。没什么了,这里:
var script = document.createElement("script");
script.src = "foo.js";
document.getElementsByTagName("body")[0].appendChild(script);
<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>