document.write alternativ而不覆盖DOM / side

时间:2014-04-01 12:20:58

标签: javascript jquery html

使用

时出现问题
  

document.write()的

我想覆盖加载adbanner的div,但我找不到任何解决方案。

<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">

<script type="text/javascript">
    var i = 0;
    var newImage;

    function refresh() {
        i++;
        if (i >= 5) {
            writeAd();
            i = 0;
        }
    }

    function writeAd() {

        var arr = box1.getElementsByTagName('script')
        for (var n = 0; n < arr.length; n++)
            eval(arr[n].innerHTML)
    }
</script>
</head>

<body
    <h1>Bilder Random</h1>
    <input type="button" onclick="refresh()" value="go!" />

    <div id="box1" class="superbanner">
        <table cellspacing="0" cellpadding="0" border="0" class="ad1">

            <script language="JavaScript">
                if (typeof (WLRCMD) == "undefined") {
                    var WLRCMD = "";
                }
                if (typeof (adlink_randomnumber) == "undefined") {
                    var adlink_randomnumber = Math
                            .floor(Math.random() * 1000000)
                }

                document
                        .write('<scr'
                                + 'ipt language="JavaScript" src="http://ad.de.doubleclick.net/adj/oms.skol.de/localnews_bilder;oms=localnews_bilder;reg=;nielsen=3b;dcopt=ist'
                                + WLRCMD + ';sz=728x90;tile=1;ord='
                                + adlink_randomnumber + '?"><\/scr'+'ipt>');
            </script>

        </table>
    </div>
</body>
</html>

我的主要问题是adbanner脚本部分中的document.write()。 我可以通过5次点击按钮重新加载页面,但它会覆盖整个DOM

是否有可能使用除document.write()之外的其他内容 在页面加载开始时和调用时插入adbanner function writeAd()

1 个答案:

答案 0 :(得分:-1)

这是你应该如何动态加载JS文件:

    if (window.isReady) { //after page was loaded, we must manually add new SCRIPT tag into HEAD
        var script = document.createElement('script'), head = document.head || document.getElementsByTagName('head')[0];
        script.type = 'text/javascript';
        script.src = fileName;
        head.appendChild(script);
    }
    else { //otherwise we are still in a progress of creating HEAD or BODY and we can simply write into document
        var write = 'write'; //prevents JSlint from saying that document.write is evil ;)
        document[write]('<script type="text/javascript" src="' + fileName + '"></script>');
    }

检测窗口是否已加载:

window.onload = function() { window.isReady = true; }