2014-12-11 53 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

相关问题