2014-12-11 44 views
0

在我的混合移動應用程序(針對android和ios開發)中,我使用以下代碼window.open("sample.html","_blank","location=yes")在inappbrowser中啓動sample.html頁面。用它打開一個inappbrowser窗口,並在url中加載帶有'X'標記的url和帶有ios底部的'完成'按鈕的sample.html頁面。如果我們點擊android中的'X'標記或ios中的'完成'按鈕,它會關閉inappbrowser。現在我希望在點擊sample.html中的按鈕時具有相同的行爲。我已經使用window.close(),但它不工作。如何在點擊inappbrowser中啓動的頁面上的按鈕時關閉phonegap inappbrowser?

我正在爲我的應用程序使用IBM Worklight框架。

回答

0

InAppBrowser是Cordova自帶的功能。
您可以閱讀更多信息,包括所有支持的event s in its documentation page

沒有直接的方式來關閉一個InAppBrowser實例「從內部」,所以它需要被解決。
這裏有一種方法來實現它:

Sample project

  1. 在你sample.html添加一些像這樣的 '關閉' 鏈接,例如:

    <!-- Point to some non-existing end-point; 
        We'll catch the error and use it to close the InAppBrowser. --> 
    <a href="/close">Close</a> 
    

    可以樣式鏈接看起來像一個按鈕,如果你真的希望它是一個'按鈕'。

  2. 在工作燈應用程式共同\ main.js添加以下,例如:

    var InAppBrowserReference; 
    
    function wlCommonInit(){ 
    
    } 
    
    function openInAppBrowser() { 
        // Use location=no,toolbar=no in order to hide the supplied 'close' buttons 
        // by the InAppBrowser instance. 
        InAppBrowserReference = window.open('sample.html', '_blank', 'location=no,toolbar=no'); 
        InAppBrowserReference.addEventListener('loaderror', closeInAppBrowser); 
    } 
    
    function closeInAppBrowser(event) { 
        if (event.url.match("/close")) { 
         InAppBrowserReference.close(); 
        } 
    } 
    

在上面的代碼段中,openInAppBrowser()從共同\的index.html爲了稱爲爲了簡單起見打開InAppBrowser。

+0

我已經嘗試過類似的選項,但它沒有爲我工作。我認爲所有分配給inappbrowser引用變量的addelistlistners都會在該事件從index.html執行但不從sample.html執行時起作用。在我的情況下,當sample.html頁面上單擊按鈕/鏈接時,必須執行loaderror事件/函數。請讓我知道,如果我不正確理解。 – user3878988 2014-12-11 20:24:53

+0

@ user3878988,請參閱示例項目的答案。 – 2014-12-11 20:34:45

+0

請注意,該應用程序是使用最近發佈的Worklight Foundation 6.3(現稱爲MobileFirst Platform Foundation)構建的。因此,如果您使用的是較舊版本的Worklight,只需將index.html,sample.html和main.js中的代碼複製到您自己的項目中即可。 – 2014-12-11 21:09:32

相關問題