Search code examples
javascriptcordovacordova-pluginsinappbrowser

How to make cordova inappbrowser wait until iframe is loaded


AFAIK, addEventListener('loadstop', ...), one of InAppBrowser API, doesn't work with iframe.

I need to my script which is executed by executeScript wait until iframe is loaded. I agree that the term 'loaded' is ambigious, so it's okay to consider it as onload event of iframe element. But I have no idea how to register an event handler (inside executeScript) and retreiving the return value from it. Because executeScript passes the value of last statement to its callback, I think there is no way to pass it outside.

ref.executeScript({code:String.raw`
   document.iframe.addEventListener("load", function(){
       //How can I pass this outside executeScript?
       document.iframe.contentDocument.innerHTML
   })
`})

Then what is the proper way to wait until iframe is loaded


Solution

  • You could do it by "polling" for the loaded value:

    Once:

    ref.executeScript({code:String.raw`
       window.iframeContent = null;
       document.iframe.addEventListener("load", function(){
           window.iframeContent = document.iframe.contentDocument.innerHTML;
       })
    `})
    

    Then Repeatedly call the following executeScript until you get a value (using setInterval or similar):

    ref.executeScript({code:String.raw`
       return window.iframeContent;
    `})