0

javascriptをinnerhtmlにロードしてスクリプトを実行する方法、私のスクリプトはhello worldのアラートではなく、openxから埋め込まれたコードです。

コード 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 1 と同じように CODE 2 を実行したいです。ありがとうございます。

4

1 に答える 1

0

getString() を eval() に入れます

div.innerHTML = eval(getstring());

コード2の例をテストするとうまくいきます。

編集:この回答に関する以下の最初のコメントを参照してください

于 2013-03-28T07:49:22.550 に答える