我正在尝试懒惰加载一些广告代码...

在页面上,我目前有一个:

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

然后,我浏览并撤出本应在页面上的所有广告,请致电他们的JavaScript Include File,这给了我这么可爱的混乱:

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 广告电话。

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

有帮助吗?

解决方案

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

document.old_write = document.write;

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

看这里: http://www.jsfiddle.net/n9hxy/

其他提示

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

DO_AD(POS)函数必须在某个地方调用。为什么不应该在哪里显示广告?

<div class="ad">
    <script>do_ad("pos_1");</script>
</div>
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top