如何将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 ('&cb=' + m3_r);
if (document.MAX_used != ',') document.write ("&exclude=" + document.MAX_used);
document.write (document.charset ? '&charset='+document.charset : (document.characterSet ? '&charset='+document.characterSet : ''));
document.write ("&loc=" + escape(window.location));
if (document.referrer) document.write ("&referer=" + escape(document.referrer));
if (document.context) document.write ("&context=" + escape(document.context));
if (document.mmm_fo) document.write ("&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 (\'&cb=\' + m3_r);'+
'if (document.MAX_used != \',\') document.write ("&exclude=" + document.MAX_used);'+
'document.write (document.charset ? \'&charset=\'+document.charset : (document.characterSet ? \'&charset=\'+document.characterSet : \'\'));'+
'document.write ("&loc=" + escape(window.location));'+
'if (document.referrer) document.write ("&referer=" + escape(document.referrer));'+
'if (document.context) document.write ("&context=" + escape(document.context));'+
'if (document.mmm_fo) document.write ("&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一样运行。 感谢。
答案 0 :(得分:0)
将你的getString()放在eval()
中 div.innerHTML = eval(getstring());
当我测试你的代码2示例时,它适用于我。
编辑:关于此答案,请在下面发表我的第一条评论