Note that there are some explanatory texts on larger screens.

plurals
  1. POPost to iframe target: cannot get response from iframe
    primarykey
    data
    text
    <p>I have a bookmarklet. When it is clicked (on any page):</p> <ol> <li>it inserts a script element with src from mypage.com. This src is downloaded.</li> <li>The src then: <ul> <li>inserts an iframe (DOM)</li> <li>inserts a form (DOM). The form targets the iframe.</li> <li>Adds data to the form and submits it to mypage.com </li> </ul></li> <li>mypage.com sends back a response: <code>&lt;div id = "msg"&gt;Message back&lt;/div&gt;</code></li> <li>This response div is inserted into the iframe. (verified in Chrome elements tab)</li> <li>Now I want to alert the returned msg "Message back". It's not working.</li> </ol> <p>When creating the iframe, I have set its <code>onload</code> event:</p> <pre><code>var i = document.createElement('iframe'); i.setAttribute('id', 'frame-id'); i.setAttribute('onload', 'iframeFormSubmitted();'); </code></pre> <p>I then have the function:</p> <pre><code>function iframeFormSubmitted(){ // in try-catch, alerting error var iframe = document.getElementById("frame-id"); var iframeDocument = iframe.contentDocument || iframe.contentWindow.document; var msg = iframeDocument.getElementById("msg").innerHTML; alert(msg); </code></pre> <p>}</p> <p>This ends up being called twice (first time, I think, because I also write the form to the iframe). The errors are:</p> <pre><code>cannot read property innerHTMl of null cannot call method getElementById of undefined </code></pre> <p>Edit: contentDocument is undefined. I'm thinking it's a cross domain thing: <a href="https://stackoverflow.com/questions/9451194/how-can-an-iframe-that-has-content-have-an-undefined-contentdocument-object">How can an iFrame that has content, have an undefined contentDocument object?</a></p>
    singulars
    1. This table or related slice is empty.
    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