 <p>You can add directly the content string</p> <p><a href="http://jsfiddle.net/0m5axpxx/" rel="nofollow">http://jsfiddle.net/0m5axpxx/</a></p> <pre><code>var iframe = document.createElement('iframe'); var html = '&lt;body&gt;&lt;scr'+ 'ipt&gt;alert(1)&lt;/s' + 'cript&gt;Content&lt;/body&gt;'; iframe.src = 'data:text/html;charset=utf-8,' + encodeURI(html); document.body.appendChild(iframe); </code></pre>