使用JavaScript将HTML字符串加载到iframe中

使用JavaScript将HTML字符串加载到iframe中

本文介绍了使用JavaScript将HTML字符串加载到iframe中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一串HTML标签,可以根据需要添加或更改.

I have a string of HTML tags that I can add to or change whenever I like.

"<html><body><script language="javascript" src=""></script></body></html>"

是否可以在运行时将该字符串加载到iframe中,就像它是HTML文件一样?

Is it possible to load that string at runtime into an Iframe as if it was an HTML file?

这是针对构造2的.我有一个对象,可以很好地从url加载HTML,它也可以插入HTML,并运行脚本,但不能按原样.

This is for Construct 2. I have an object that can load HTML from a url fine, it can also insert HTML, and run scripts, but not as is.

推荐答案

您可以使用

document.getElementById('iframe').src = "data:text/html;charset=utf-8," + escape(html);

有关示例,请参见以下小提琴

See the following fiddle for an example

https://jsfiddle.net/erk1e3fg/

这篇关于使用JavaScript将HTML字符串加载到iframe中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-06 23:46