0

I'm trying to leverage modules to dynamically attach variables on iframe's Window.

The problem is, that when I'm doing simple iframe.contentDocument.write(html), with script of type='module', the script inside iframe won't run. If i use script without type='module', the script runs (and for example logs something to console)

Why am I unable to inject module scripts into an iframe like this? Is this some layer of security I am not aware of?

Edit: Here is piece of code i'm injecting, i checked the network tab, and browser fetches the dependency from public directory, console.log() doesn't run though

<script type="module">
      import * as materialuicore from "/.vite/@material-ui_core.js"
      console.log('materialuicore')
      if (!window.__deps) window.__deps = {}

      window.__deps["@material-ui/core"] = materialuicore
    </script>
Martin Mecir
  • 80
  • 1
  • 8
  • `contentDocument.write(html)` - why are you using that awful, deprecated, atrocious function? – Dai Jun 17 '21 at 05:37
  • "Why i am unable to inject module scripts into iframe like this?" JavaScript modules don't run inline in a page, so there is no still-loading document content stream to write raw text into. – Dai Jun 17 '21 at 05:37
  • https://stackoverflow.com/questions/802854/why-is-document-write-considered-a-bad-practice – Dai Jun 17 '21 at 05:38
  • @Dai i updated the info. Looks like the module scripts won't run when injected with document.write(), not only inside iframe, but on main window too – Martin Mecir Jun 17 '21 at 06:41

0 Answers0