0

Fixing the following javascript code contains my newest task:

// submit the form in the iframe:
document.getElementsByTagName("iframe")[0].contentWindow.dojo.byId(buttonId).click(); 
// wait 50msec for result to be loaded:
window.setTimeout(function(){
    // get documentId of newly created document from result
    var docId = document.getElementsByTagName("iframe")[0].
                    contentWindow.dojo.byId("documentIdDiv").innerHtml;
    // put documentId into the outer form
    Ext.getCmp("form").getForm().findField("documentId").setValue(docId);
    // submit outer form
    Ext.getCmp("form").getForm().submit({
        ...
    });
}, 50);
// TODO: Error if result is not available after 50ms.

I now have to find a way to execute the anonymous function once the iframe has reloaded.

I thought of using a body onload on the iframe, but I can't, because I would have to insert it in the body of the new page.

Alexander
  • 19,906
  • 19
  • 75
  • 162
  • You might want to take a look at https://github.com/wingify/please.js if you have complex interactions with iframes – apsdehal Dec 18 '14 at 13:14
  • 1
    Use this [IFrame Callback](http://stackoverflow.com/questions/164085/javascript-callback-when-iframe-is-finished-loading) – Roger Dec 18 '14 at 13:14
  • @Neoaptt As far as I see, the solution would require jQuery, which I don't have. Could you make an answer elaborating on that, and include a JavaScript-only version? – Alexander Dec 18 '14 at 13:58

1 Answers1

1

Haha, trying to get this to work on JSFiddle was more difficult. Since they load javascript seperately from html. Anyways this should work.

DEMO

//function that happens when loaded
function loaded() {
    b.innerHTML = 'Call Back Worked';
};
//gets elements and sets them to variables
var b = document.getElementById('callback')
var a = document.getElementById("myframe");
//listens to load event
a.addEventListener("load", loaded());
Roger
  • 3,226
  • 1
  • 22
  • 38
  • Sorry, but this can't be working because even if you don't load the iFrame, "Callback worked" is written. Because loaded() is executed and the result is put into the event listener. – Alexander Dec 18 '14 at 14:55
  • But I will try with addEventListener for myself. The problem of JSfiddle is that XSS is blocked in most browsers. – Alexander Dec 18 '14 at 14:56
  • I got it working using `iframe.addEventListener("load",function() { iframe.contentWindow.document.getElementById(...)... });` – Alexander Dec 18 '14 at 16:08