有没有办法拦截`document.write`?

Sea*_*nJA 2 javascript document.write advertisement-server

我试图懒惰加载一些广告服务器代码...

在页面上,我现在有这个:

<div class="ad">
    <span>pos_1</span>
</div>
Run Code Online (Sandbox Code Playgroud)

然后我通过并删除应该在页面上的所有广告,调用他们的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;
    }
}
Run Code Online (Sandbox Code Playgroud)

然后,我想用document.write广告调用的结果替换范围.

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

Mat*_*att 6

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

document.old_write = document.write;

document.write = function (str) {
    // lalala
};
Run Code Online (Sandbox Code Playgroud)

见这里:http://www.jsfiddle.net/N9hXy/