2009-11-09 17 views
0

分配此後:window.onload = initfunction;動態的Javascript附加location.href屬性和ajax-cross-domain.com腳本

我想了AJAX跨域腳本追加到標題:

 
function initfunction() { 
var dh = document.getElementsByTagName('head')[0]; 
var script = null; 
script = document.createElement('script'); 
script.setAttribute('src', 'http://whatever.com/cgi-bin/ACD/ACD.js?'+location.href); 
script.setAttribute('type', 'text/javascript'); 
dh.appendChild(script); 
    } 

腳本似乎與正確的域名追加,但螢火蟲說:「無法加載源」。如果我在src屬性中輸入一個固定的URL,它會起作用!例如: -

script.setAttribute('src', 'http://whatever.com/cgi-bin/ACD/ACD.js?http://google.com');

任何想法?

+0

你試過alerting location.href嗎?是否給你預期的結果? – karim79 2009-11-09 14:03:38

回答

0

好的我得到了解決方案。問題不是「location.href」本身,而是我們的防火牆中的一個規則,該規則禁止向自己的服務器發出GET請求。因此腳本拋出超時。

0

假設我們正在談論ajax-cross-domain.com的劇本,應該不會是:

script.setAttribute('src', 'http://whatever.com/cgi-bin/ACD/ACD.js?uri=('+encodeURIComponent(location.href)+')'); 
+0

沒錯,但是我簡化了腳本(剪掉了uri =東西等等)。但是,使用固定的URI就行了! – dforce 2009-11-09 14:17:23

+0

你在'@ allowed_uris'裏面包含了正確的uri = ...版本嗎? – bobince 2009-11-09 14:20:09

+0

另外,你試圖使用的是什麼網址?通過ACD代理獲取當前頁面('location.href')是毫無意義的。 – bobince 2009-11-09 14:22:15

0

下面是用於測試目的的簡化代碼段。只需將它作爲頭文件中的onload函數或腳本標籤即可。該網頁將連續加載...

 
var dh = document.getElementsByTagName('head')[0]; 
if(!dh) 
{ 
    //html page without "head" 
    head = document.createElement('head'); 
    document.appendChild(head); 
} 
var script = null; 
script = document.createElement('script'); 
script.setAttribute('src', 'http://domain.com/cgi-bin/ACD/ACD.js?' + location.href); 
script.setAttribute('type', 'text/javascript'); 
dh.appendChild(script);