Wait, are you really needing to render it using javascript?
Be aware that in HTML5 there is srcdoc
, which can do that for you! (The drawback is that IE/EDGE does not support it yet https://caniuse.com/#feat=iframe-srcdoc)
See here [srcdoc
]: https://www.w3schools.com/tags/att_iframe_srcdoc.asp
Another thing to note is that if you want to avoid the interference of the js code inside and outside you should consider using the sandbox
mode.
See here [sandbox
]: https://www.w3schools.com/tags/att_iframe_sandbox.asp