【问题标题】:Opening two instances of InAppBrowser (_system and _blank) prevents events from triggering打开 InAppBrowser 的两个实例(_system 和 _blank)可防止触发事件
【发布时间】:2016-10-05 19:00:40
【问题描述】:

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

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

这是实际代码的样子。

// Opening iab main window
var ref = window.open(global.chat_mediador, '_blank','location=no,toolbar=yes');

var handleEvents =  function(event) {

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

    // Trigger custom event
    if (event.url.match('#openccard')) {
        window.open('https://www.test.example.url.com?customerID=' + event.customerId, '_system', 'location=yes');
    }

}

// InAppBrowser events

// This events are duplicated because loadstop works on android and
// loadstart works on ios.
ref.addEventListener('loadstart', handleEvents, false);
ref.addEventListener('loadstop', handleEvents, false);

// Removing the dialog when we close the chat
ref.addEventListener('exit', function(event) {
    generali.dialog.close();
}, false);

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

我们尝试过(没有运气):

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

window.open(global.url_ficha + customerId, '_system','location=no');
var cardsRef = window.open(
    'https://www.test.example.url.com?customerID=' + customerId,
    '_system', 
    'location=yes'
);         

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

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

有人知道发生了什么吗?

【问题讨论】:

    标签: javascript cordova inappbrowser


    【解决方案1】:

    似乎每次执行 new 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;
    };
    

    【讨论】:

    猜你喜欢
    • 2017-03-24
    • 2019-02-01
    • 1970-01-01
    • 2010-11-12
    • 2015-03-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2010-10-27
    相关资源
    最近更新 更多