打开 InAppBrowser 的两个实例(_system 和 _blank)可防止事件触发

Ion*_*has 4 javascript cordova inappbrowser

我们\xe2\x80\x99 目前正在开发一个带有 cordova 和 InAppBrowser 插件的应用程序。我们尝试同时生成两个不同的 IAB 实例。一个使用 _system 浏览器,另一个使用 _blank 选项。

\n\n

我们遇到的问题是,一旦我们打开 _system 浏览器的实例,我们似乎就失去了对先前浏览器的引用。因此,在 _system 浏览器关闭后,_blank IAB 上永远不会触发 close 事件。

\n\n

这就是实际代码的样子。

\n\n
// Opening iab main window\nvar ref = window.open(global.chat_mediador, \'_blank\',\'location=no,toolbar=yes\');\n\nvar handleEvents =  function(event) {\n\n    // Closing the iab window \n    if (event.url.match(\'#close\')) {\n        ref.close();\n    }\n\n    // Trigger custom event\n    if (event.url.match(\'#openccard\')) {\n        window.open(\'https://www.test.example.url.com?customerID=\' + event.customerId, \'_system\', \'location=yes\');\n    }\n\n}\n\n// InAppBrowser events\n\n// This events are duplicated because loadstop works on android and\n// loadstart works on ios.\nref.addEventListener(\'loadstart\', handleEvents, false);\nref.addEventListener(\'loadstop\', handleEvents, false);\n\n// Removing the dialog when we close the chat\nref.addEventListener(\'exit\', function(event) {\n    generali.dialog.close();\n}, false);\n
Run Code Online (Sandbox Code Playgroud)\n\n

正如您所看到的,我们使用 _blank 选项打开应用程序中的第一个 url。然后,如果在子应用程序中按下按钮,我们希望在 _system 浏览器中打开浏览器的实例。

\n\n

我们\xe2\x80\x99已经尝试过(没有运气):

\n\n

为 _system 浏览器提供单独的参考。

\n\n
window.open(global.url_ficha + customerId, \'_system\',\'location=no\');\nvar cardsRef = window.open(\n    \'https://www.test.example.url.com?customerID=\' + customerId,\n    \'_system\', \n    \'location=yes\'\n);         \n
Run Code Online (Sandbox Code Playgroud)\n\n

在_blank浏览器的引用之外触发自定义事件

\n\n
 if (event.url.match(\'openccard\')) {\n     var customerId = event.url.split(\'openccard-\')[1];\n     var evt = document.createEvent("Event");\n     evt.initEvent("openccard",true,true);\n     evt.customerId = customerId;\n     document.dispatchEvent(evt);\n }\n
Run Code Online (Sandbox Code Playgroud)\n\n

有人知道发生了什么吗?

\n

Ion*_*has 5

似乎每次执行新的 window.open() 时都需要初始化 IAB,如果不这样做,事件侦听器将不起作用。

如果我使用该代码,它就会像魅力一样发挥作用。

window.openIAB = function(url, target, options) {

    var self = this;
    var ref = window.open(url, target, options);

    var handleChildEvents = function(ev) {

        if (ref != undefined) {

            // Closing the iab window 
            if (ev.url.match('#close')) {
                ref.close();
                ref = undefined;
            }

            // Opening card url with system browser
            if (ev.url.match('#openccard')) {
                var customerId = ev.url.split('#openccard-')[1];
                self.ref2 = self.openIAB(
                    'https://www.test.com?customerID=' + customerId,
                    '_system', 
                    'location=yes'
                );
            }

        } else {
            console.log('InAppBrowser has no reference');
        }

    };

    ref.addEventListener('loadstart', handleChildEvents);
    ref.addEventListener('loadstop', handleChildEvents);

    ref.addEventListener('loaderror', function(ev) {
        console.log('error while loading page');
        ref.close();
        ref = undefined;
    });

    ref.addEventListener('exit', function(ev) {
        dialog.close();
    });

    return ref;
};
Run Code Online (Sandbox Code Playgroud)