I have a few problems with iframe usage. I'll ask one question at a time. I am working on the LaTeX.js playground and wanted to add some JavaScript to process \marginpars, but whatever I do, neither Chrome nor Firefox even attempt to load the script. It works if the exact same code is used outside of any frames.
I tried both absolute and relative paths, and I tried file, http, and https protocols. Always the same result.
Here is the relevant code:
<iframe id="preview" sandbox="allow-same-origin allow-scripts"> <html> <head> <title>LaTeX.js Show­case</title> <meta charset="UTF-8"> <link type="text/css" rel="stylesheet" href="css/katex.css"> <link type="text/css" rel="stylesheet" href="css/article.css"> <link type="text/css" rel="stylesheet" href="css/error.css"> <script src="https://latex.js.org/js/base.js"></script> </head> ... The problem is base.js. The developer tools show no error in the console, no entry in the sources tab, and no entry in the network tab.
UPDATE: as I said in the comments, the code above is the result from modifying the DOM using JavaScript; however, setting iframe.contentWindow.document.documentElement does not seem to trigger loading the scripts in <head>, only Option 2 in the answer works for me.