2017-02-13 62 views
0

AFAIK,addEventListener('loadstop', ...),InAppBrowser API之一,不能與iframe一起使用。如何使科爾多瓦inappbrowser等到iframe加載

我需要我的腳本執行executeScript等到iframe加載。我同意「加載」這個詞很模糊,因此可以將其視爲iframe元素的onload事件。但我不知道如何註冊一個事件處理函數(在executeScript中)並從中獲取返回值。因爲executeScript將最後一條語句的值傳遞給它的回調函數,所以我認爲沒有辦法將它傳遞到外部。

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

那麼什麼是正確的方式要等到iframe中加載

回答

0

您可以通過 「輪詢」 爲裝載值做到這一點:

一旦:

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

然後重複調用以下executeScript,直到獲得值(使用setInterval或類似的值):

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