Embedding HTML code with verbatim string and not file name

Tags: ,



I am looking for a way to embed HTML code in a page. I already know that the embed element can do this for me:

<!DOCTYPE html>
<html>
<body>

<h1>The embed element</h1>

<embed type="text/html" src="snippet.html"  width="500" height="200">

</body>
</html>

However, the embed element takes an html file as src. I am wondering if I could pass a verbatim string (as the HTML code) to the embed element. Is this doable with any other mechanism?

My goal is to be able to change the verbatim string dynamically with a JS code, so that the generated embedded HTML changes dynamically.

Answer

You can add your html code to the srcdoc of an iframe:

<iframe srcdoc="<html><body><h1>Hello World!</h1></body></html>" />


Source: stackoverflow