1

A common question that I am struggling to make work -

Task:

Keyboard actions served to different pieces of my page

  • index.html using "<body onkeydown="onkeydown_resp()">"
  • onkeydown_resp() is in utils.js
  • page is hosting an iframe with projects.html
  • uses keyboard to stop/start some media

Need:

JavaScript state changes shared across Js & HTML content

e.g. onkeydown_resp() may -

  • Trigger the loaded iframe of projects.html to toggle to a new project (Key1)

  • Stop an animation for one of those project demos (Key2)

  • And toggle some audio (Key3)

I have tried a plethora of options to no success. I am just looking to learn & establish a portfolio demo for www.justinreina.com.

Question:

  • How can I get this task to work?

        Expected - create a global 'var' or window.myVar and share it

        Tried - None of these seem to work 1/2/3/4/5/6/7

J-Dizzle
  • 4,861
  • 4
  • 40
  • 50
  • 1
    It seems your question is about interacting with the iframe, but none of the linked Q&A say a thing about iframes. Perhaps you should search for "sharing state with iframe"? – Randy Casburn Nov 13 '21 at 23:01
  • 2
    This may help: https://developer.mozilla.org/en-US/docs/Web/HTML/Element/iframe#scripting – Randy Casburn Nov 13 '21 at 23:03
  • 1
    You might want to check [out namespacing too](https://www.oreilly.com/library/view/learning-javascript-design/9781449334840/ch13s15.html). We don't tend to use it too much these days, but it was very useful. – Andy Nov 13 '21 at 23:05
  • 1
    An iFrame is a separate document and does not have access to any of the properties defined on the parent window object. Use cross messaging as described here https://developer.mozilla.org/en-US/docs/Web/API/Window/postMessage and here https://stackoverflow.com/a/9154267/12291657 – aside Nov 13 '21 at 23:05
  • 2
    Does this answer your question? [How to communicate between iframe and the parent site?](https://stackoverflow.com/questions/9153445/how-to-communicate-between-iframe-and-the-parent-site) – aside Nov 13 '21 at 23:07
  • you guys are amazing, thank you. I am learning much here I will process this and get back to you (wow - did not expect this!) – J-Dizzle Nov 14 '21 at 06:46

0 Answers0