javascript
2013-12-09 50 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); 
}; 
相关问题