Note that there are some explanatory texts on larger screens.

plurals
  1. POHow to share a data between a window and a frame in JavaScript
    primarykey
    data
    text
    <p>This is WebKit browsers specific (meaning that I only need to make it work in WebKit specific, i.e. iOS/Android browsers, but I'm testing in Chrome).</p> <p>I have a page. The page loads one or more iframes, with contents from another domain. I need to receive messages (using postMessage()) from these iframes, and I need to be able to identify which iframe a specific message came from.</p> <p>I can't find a way to do that that does not involve throwing something the iframe URL that the iframe contents then can pass back to me. I would like to not have to meddle with the URL, as there is no guarantee I can safely do that (redirects can throw the parameters out, for example).</p> <p>I tried something that I thought was reasonable. When I create the iframe element (it's done from J/S), I associated a property with the element, let's say 'shared_secret'. When I get the message event back from the frame, I tried to locating the element that the calling frame was created with, and reading that property.</p> <pre><code>function onMessage(evt) { var callerId = evt.source.frameElement.shared_secret; // .... } window.addEventListener(message, onMessage); var frameEl = document.createElement('iframe'); frameEl.shared_secret = 'sommething blue'; frameEl.src = 'http://aliens.com/my.html'; somewhereInMyDoc.appendChild(frameEl); </code></pre> <p>When the frame loads, it will run:</p> <pre><code>window.parent.postMessage('do you know who I am?', '*'); </code></pre> <p>However, frameElement turns out undefined in the above onMessage(). I guess for the security reasons, it does work perfectly when the parent/child are from the same domain.</p> <p>And it's actually ironic. Parent window can not access event.source.frameElement because event.source is an alien window. iFrame window can not call window.frameElement, because frameElement is in an alien window. So nobody can get access to it.</p> <p>So, is there something that I can use as a token that I can set on a newly loaded frame, and somehow get back?</p> <p>Thank you.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload