javascriptjqueryhtmliframe

How do I set content of iframe with Javascript?


I have the following HTML structure.

<iframe id="myiframe" src='myiframeContent.html'></iframe> 

and I have the following JavaScript variable with an HTML string:

var s ="<html><head></head><body><div>Test_Div</div></body></html>";

How can I change the content of iframe with id myiframe with variable s?


Solution

  • I managed to do it with

    Javascript Code:

      let html_string= "content";
      let myIframe = document.getElementById('output_iframe1');
      myIframe.src = `data:text/html;charset=utf-8,${html_string}`;
    

    HTML code:

    <iframe id="output_iframe1"></iframe>