Note that there are some explanatory texts on larger screens.

plurals
  1. POLoading a HTML snippet into a dynamically created iframe
    text
    copied!<p>I am trying to load a HTML snippet into a newly created iframe in the following.</p> <pre><code>&lt;!DOCTYPE html&gt; &lt;html&gt; &lt;head&gt;&lt;title&gt;Test&lt;/title&gt;&lt;/head&gt; &lt;body&gt; &lt;script type="text/javascript"&gt; var iframeId = "frame" + Math.floor((Math.random() + "") * 1000000000000).toString(); document.write('&lt;iframe height="300"' + ' width="300"' + ' frameborder="0"' + ' scrolling="no"' + ' id="' + iframeId + '"' + ' name="' + iframeId + '"' + ' allowtransparency="true"&gt;&lt;/iframe&gt;'); var iframe = document.getElementById(iframeId); var iframeContent = '&lt;!-- --\&gt;&lt;script language=\"javascript\"\&gt;&lt;/script\&gt;'; iframe.contentWindow.document.open(); iframe.contentWindow.document.write( '&lt;!DOCTYPE html\&gt;&lt;html\&gt;&lt;head\&gt;&lt;title\&gt;Test&lt;/title\&gt;&lt;/head\&gt;&lt;body\&gt;' + iframeContent + '&lt;/body\&gt;&lt;/html\&gt;'); iframe.contentWindow.document.close(); &lt;/script&gt; &lt;/body&gt; &lt;/html&gt; </code></pre> <p>When I open this document in a browser, the iframe is not loaded properly. Any thoughts on what might be causing this?</p> <p>If I set iFrameContent as</p> <pre><code>var iframeContent = '&lt;!-- --\&gt;'; </code></pre> <p>or</p> <pre><code>var iframeContent = '&lt;script language=\"javascript\"\&gt;&lt;/script\&gt;'; </code></pre> <p>the page seems to load properly.</p> <p>iframeContent is essentially a third party HTML snippet (escaped using freemarker js_string) and may contain HTML comments.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload