javascript
2013-03-28 68 views 0 likes 
0

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

代碼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> 

CODE 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運行相同代碼1. 感謝。

+0

這就是爲什麼你不應該重複發帖!還有,存在document.body – MaxArt 2013-03-28 08:41:45

回答

0

把你的getString()用eval()

div.innerHTML =的eval(GetString的());

它適用於我當我測試你的代碼2的例子。

編輯:下面瑟我的第一個評論這個答案

+0

你的瀏覽器是什麼?我不能運行它,我得到的消息undefined在佈局 – 2013-03-28 08:49:48

+0

我不得不重新考慮,因爲我的回答很快。我得到了要呈現的橫幅,但不是您的'橫幅'分區。它呈現在下面,我也得到未定義的字符串在div中。您在mysting變量中的腳本是否是靜態的並從某處收到? – RoggA 2013-03-28 10:11:32

相關問題