Javascript 如何从HTML字符串向网页动态添加脚本?
假设我有一个名为html的字符串,其中包含以下内容:Javascript 如何从HTML字符串向网页动态添加脚本?,javascript,html,dom,iframe,Javascript,Html,Dom,Iframe,假设我有一个名为html的字符串,其中包含以下内容: <script> document.write("Some random stuff here"); </script> <script src="someremotejsfile"></script> 但这会在firefox中造成问题,在firefox中,微调器会一直旋转,好像它的加载永远不会停止,即使内容已经加载。我的下一次尝试是: document.body.innerHTML
<script>
document.write("Some random stuff here");
</script>
<script src="someremotejsfile"></script>
但这会在firefox中造成问题,在firefox中,微调器会一直旋转,好像它的加载永远不会停止,即使内容已经加载。我的下一次尝试是:
document.body.innerHTML = html;
这会将脚本添加到主体中,但实际上不会执行它们。所以最后我试着:
div = document.createElement("div");
div.innerHTML = html;
document.body.appendChild(div);
但这似乎也不会执行html字符串中的脚本
所以我的问题是,给定一个html字符串,如何动态地将其添加到页面中?例如,它可以是一个广告标签,其中包含任意数量的脚本和其他html元素。我无法控制html字符串中包含的内容。这对我来说是个黑匣子。我只需要能够获取那长串html并将其加载到窗口中(在本例中为iframe)。如果使用jQuery,则可以使用.html加载,它将启动脚本
$(document.body).html( $(document.body).html() + htmlToAdd );
如果不使用jQuery,可以手动评估脚本
function appendHTMLtoBody(html){
var body = document.body;
var scriptsLoaded = [].slice.apply(body.getElementsByTagName("script"),[0]);
for(var i = 0; i < scriptsLoaded.length; i++){
scriptsLoaded[i].setAttribute("data-loaded","true");
}
body.innerHTML += html;
var allScripts = body.getElementsByTagName("script");
for(var i = 0; i < allScripts.length; i++){
if( allScripts[i].getAttribute("data-loaded") !== "true" ){
var script = allScripts[i].innerHTML;
eval(script);
}
}
}
函数appendHtmlTody(html){
var body=document.body;
var scriptsLoaded=[].slice.apply(body.getElementsByTagName(“脚本”),[0]);
for(var i=0;i
我想这会解决你的问题。document.write-works:
<iframe id="ifr"></iframe>
<script type="text/javascript">
var scr = decodeURIComponent("%3Ch1%3EHello%20World%3C%2Fh1%3E%3Cscript%3Ealert(%27Some%20random%20stuff%20here%27)%3B%3C%2Fscript%3E");
document.getElementById("ifr").contentWindow.document.write(scr);
document.getElementById("ifr").contentWindow.document.close();
</script>
var scr=decodeURIComponent(“%3Ch1%3EHello%20World%3C%2Fh1%3E%3Cscript%3Ealert(%27Some%20random%20stuff%20here%27)%3B%3C%2Fscript%3E”);
document.getElementById(“ifr”).contentWindow.document.write(scr);
document.getElementById(“ifr”).contentWindow.document.close();
(别管编码的URI字符串,只需要它能够将代码
Hello Worldalert(“这里有些随机的东西”);
分配给脚本标记内部的字符串变量我有限的知识表明这是不可能的。你不能越接近于只评估脚本(没有标记)例如,在字符串中。我希望我错了,因为这很有趣。+1所以这个名为“html”的字符串是一个黑匣子……但它是整个网页(包括html和head元素)吗或者它只是部分html?它是否包含您想要执行的脚本?我认为您必须将“html”字符串分离…一个有用的网站:。我偶尔会提到它。可能看起来很奇怪,但您可能可以文档。将这个“黑框”写入页面…而不是$(document.body).html($(document.body).html($(document.body).html()+htmlToAdd);,您还可以使用append函数:$(document.body).append(htmlToAdd);或prepend,如果您想将其插入到前端,这似乎也可以,尽管JQuery不是我正在使用的选项。Document.write在Chrome上非常适合我。但是,在firefox上,Document.write使窗口似乎永远加载,就像页面顶部的微调器永远旋转一样。内容加载正确,但TF无法确定内容实际加载的时间。即使是一个简单的document.write(html)在iframe窗口中,html=“Hello World”也会导致此情况发生。添加document.close()
似乎可以解决此问题(更新了我的答案).我知道你以前试过,但你能用上面的解决方案再试一次吗?因此,如果我从你的帖子中保留警报(“随机的东西”),它就会工作。Firefox会像加载一样旋转,一旦我退出警报窗口,页面就会加载。但是,如果我去掉警报语句,只留下“Hello World”,Firefox永远不会停止加载。你确定吗?作为独立脚本和这里似乎都正常:也许缓存是问题所在?是的,这是有效的。有一个我不知道的随机脚本导致FF永远加载。一旦我摆脱它,这个方法就行了。Thnks!
<iframe id="ifr"></iframe>
<script type="text/javascript">
var scr = decodeURIComponent("%3Ch1%3EHello%20World%3C%2Fh1%3E%3Cscript%3Ealert(%27Some%20random%20stuff%20here%27)%3B%3C%2Fscript%3E");
document.getElementById("ifr").contentWindow.document.write(scr);
document.getElementById("ifr").contentWindow.document.close();
</script>