0

The following code throws "Uncaught TypeError: Cannot read properties of undefined (reading 'undefined')" when it should show 123. What am I doing wrong? I asked a similar question here Symbols and cross realms but nobody answered my specific question about cross realms

<body>
  <iframe srcdoc="<script>var sym = Symbol(); var obj = {}; obj[sym] = 123;</script>">
  </iframe>
  <script>
    const iframe = document.querySelector('iframe');
    const content = iframe.contentWindow;
    const value = content.obj[content.sym]; // undefined
  </script>
</body>
Heretic Monkey
  • 11,687
  • 7
  • 53
  • 122
  • 1
    You'll need to wait until iframe has loaded -> `iframe.addEventListener('load', () => { do the thing here})` – Bravo Mar 23 '22 at 23:20
  • Oh, thanks, so, if I do the same with my other link question it will now show the use of Symbol.for() right? –  Mar 23 '22 at 23:27
  • You need the iframe to be loaded to use it, so, what do you think? – Bravo Mar 23 '22 at 23:28
  • Yea, thanks, it worked, what happened is that in the articles I read nobody used the "load" and as I had never used iframes I got confused, thank you very much, now I understand the use of Symbol.for() to cross realms. –  Mar 23 '22 at 23:31

0 Answers0