簡體   English   中英

在innerHTML中加載JavaScript

[英]Load javascript in innerHTML

如何將javascript加載到innerhtml中並執行腳本,我的腳本沒有警告世界,而是從openx嵌入的代碼。

CODE 1.這是我的原始代碼,此代碼將顯示橫幅:

<!DOCTYPE html>
<html>
    <head>
    </head>
    <body>
        <div id="banner">
            <script type='text/javascript'>
                <!--//<![CDATA[
                var m3_u = (location.protocol=='https:'?'https://203.130.226.231/advediax/www/delivery/ajs.php':'http://203.130.226.231/advediax/www/delivery/ajs.php');
                var m3_r = Math.floor(Math.random()*99999999999);
                if (!document.MAX_used) document.MAX_used = ',';
                document.write ("<scr"+"ipt type='text/javascript' src='"+m3_u);
                document.write ("?zoneid=1");
                document.write ('&amp;cb=' + m3_r);
                if (document.MAX_used != ',') document.write ("&amp;exclude=" + document.MAX_used);
                document.write (document.charset ? '&amp;charset='+document.charset : (document.characterSet ? '&amp;charset='+document.characterSet : ''));
                document.write ("&amp;loc=" + escape(window.location));
                if (document.referrer) document.write ("&amp;referer=" + escape(document.referrer));
                if (document.context) document.write ("&context=" + escape(document.context));
                if (document.mmm_fo) document.write ("&amp;mmm_fo=1");
                document.write ("'><\/scr"+"ipt>");
                //]]>-->
            </script>
        </div>
    </body>
</html>

代碼2。這是我的轉換代碼。 但是不行,我想在上面的代碼中顯示橫幅:

<!DOCTYPE html>
<html>
    <head>
    </head>
    <body>
        <script>
            function getstring(){
                var mystring = '<!--//<![CDATA \nvar m3_u = (location.protocol=="https:"?"https://203.130.226.231/advediax/www/delivery/ajs.php":"http://203.130.226.231/advediax/www/delivery/ajs.php");'+
                    'var m3_r = Math.floor(Math.random()*99999999999);'+
                    'if (!document.MAX_used) document.MAX_used = ",";'+
                    'document.write ("<scr"+"ipt type=\'text/javascript\' src=\'"+m3_u);'+
                    'document.write ("?zoneid=1");'+
                    'document.write (\'&amp;cb=\' + m3_r);'+
                    'if (document.MAX_used != \',\') document.write ("&amp;exclude=" + document.MAX_used);'+
                    'document.write (document.charset ? \'&amp;charset=\'+document.charset : (document.characterSet ? \'&amp;charset=\'+document.characterSet : \'\'));'+
                    'document.write ("&amp;loc=" + escape(window.location));'+
                    'if (document.referrer) document.write ("&amp;referer=" + escape(document.referrer));'+
                    'if (document.context) document.write ("&context=" + escape(document.context));'+
                    'if (document.mmm_fo) document.write ("&amp;mmm_fo=1");'+
                    'document.write ("\'><\/scr"+"ipt>");'+
                    '//]]>-->';
                return mystring;
            }

            var div = document.createElement("div");
            div.id="banner";
            div.innerHTML = getstring();
            div.style.cssText = "padding: 10px; border:1px solid; background-color: #000; color: #fff;";
            document.getElementsByTagName("body")[0].appendChild(div);
            document.getElementsByTagName("body")[0].insertBefore(
            document.getElementById("banner"),
            document.getElementsByTagName("body")[0].firstChild);

            var scripts = getstring();
            for (var i = 0; i < scripts[i].length; i++) {
                eval(scripts[i].text);
            }

        </script>
    </body>
</html>

我想讓CODE 2與CODE 1一樣運行。謝謝。

將您的getString()放在eval()中

div.innerHTML = eval(getstring());

當我測試您的代碼2示例時,它對我有用。

編輯:se我下面對此答案的第一條評論

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM