javascript
2013-12-09 52 views 0 likes 
0

我有一些跟蹤代碼,我必須將其嵌入到頁面的頁腳中。跟蹤代碼來自第三方公司,並使用document.write,這當然是阻止的。我向公司發送了支持請求,他們似乎沒有任何興趣來更改代碼。document.write異步(JavaScript)

跟蹤代碼看起來是這樣的:

document.write (
    '<img src="http://company-url.com/visitor.gif?ts='+ 
    new Date().getTime()+ 
    '&ref='+escape(document.referrer) + '">' 
); 

所以,我的問題是,是否有可能使document.write非阻塞?

+0

你是什麼意思?你能不能把它重寫成'document.getElementById('footer')。innerHTML ='''? – putvande

+1

如果我有選擇,我不會在*全部*處使用'document.write'! – MackieeE

+0

所以你知道,它不是'document.write',而是所有的腳本執行都被阻塞,除非你使用像'defer'或'async'這樣的東西。但是'document.write'不再有效。 – Jacob

回答

1

不,不可能使document.write異步。但你可以在頁面加載後將一個img附加到body上:

window.onload = function() { 
    var url = 'http://company-url.com/visitor.gif?ts='+new Date().getTime()+'&ref='+escape(document.referrer); 
    var img = document.createElement('img'); 
    img.src = url; 
    document.body.appendChild(img); 
}; 
相關問題