2

我正在尝试延迟加载一些广告服务器代码...

在页面上我现在有这个:

<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广告调用的结果替换跨度。

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

4

3 回答 3

6

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

document.old_write = document.write;

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

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

于 2011-01-20T19:52:24.977 回答
1
document.write = function(str) {
    window.buf += str;
}
于 2011-01-20T19:51:54.460 回答
0

do_ad(pos) 函数必须在某处调用。为什么不应该在哪里展示广告?

<div class="ad">
    <script>do_ad("pos_1");</script>
</div>
于 2018-01-03T14:09:30.157 回答