2011-01-20 59 views
2

我试图延迟加载一些广告服务器代码...有没有办法拦截`document.write`?

在网页上我有这样的时刻:

<div class="ad"> 
    <span>pos_1</span> 
</div> 

我然后再通过并拉出所有的广告应该是对的页面,称他们的JavaScript包含文件,这让我这个可爱的烂摊子:

function do_ad(pos){ 
    switch(pos){ 
     case 'pos_1': 
      document.write('first ad text'); 
      document.write('first ad more text'); 
      //and so on for many many lines 
      break; 
     case 'pos_2': 
      document.write('second ad text'); 
      document.write('second ad more text'); 
      //and so on for many many lines 
      break; 
    } 
} 

我再想用document.write广告调用的结果来替换跨度。

有没有办法让它返回已写入页面的字符串?

+0

的可能重复的[JavaScript的 - 控制装置,用于文件撰写插入点](http://stackoverflow.com/questions/1536970/javascript-controlling-the-insertion-point-for-document-write) –

回答

6

我不明白为什么你不能覆盖document.write功能:

document.old_write = document.write; 

document.write = function (str) { 
    // lalala 
}; 

在这里看到:http://www.jsfiddle.net/N9hXy/

+0

谢谢!现在我必须处理脚本中的其他缺陷...叹息 – SeanJA

+1

唉......这并不像我想要的那样工作......因为广告服务器是三层深的:'document.write(' '))';'。尽管如此,它对单一深度的效果很好。 – SeanJA

1
document.write = function(str) { 
    window.buf += str; 
} 
+0

+1此外,请务必在此之前定义'window.buf'。 –

0

的do_ad(POS)函数必须在某处被调用。为什么不在广告的展示位置?

<div class="ad"> 
    <script>do_ad("pos_1");</script> 
</div> 
相关问题