打开InAppBrowser(_system和_blank)的两个实例可防止事件触发 [英] Opening two instances of InAppBrowser (_system and _blank) prevents events from triggering

查看:2173
本文介绍了打开InAppBrowser(_system和_blank)的两个实例可防止事件触发的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

We’re currently developing an app with cordova and the InAppBrowser plugin. We're trying to spawn two different IAB instances at the same time. One with the _system browser and another with the _blank option.

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

The problem we have is that once we open the instance of _system browser, it seems we lose the reference to the previous browser. For this reason, the close event never triggers on the _blank IAB after the _system browser is closed.

这是实际代码的外观。

// 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浏览器中打开一个浏览器实例。

As you can see we open the first url within the application with the _blank option. Then if in the child application a button is pressed we want to open an instance of a browser in the _system browser.

我们试过(没有运气):

We’ve tried (without luck) to:

为_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);
 }

任何人都知道发生了什么?

Anyone has an idea of what's happening?

推荐答案

看来你需要初始化IAB每次你做一个新的window.open()如果你不这样做的事件监听器不

It seems that you need to initialize the IAB each time you do a new window.open() if you don't do that the event listeners don't work.

如果我使用该代码,它就像一个魅力。

If I use that code it works like a charm.

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;
};

这篇关于打开InAppBrowser(_system和_blank)的两个实例可防止事件触发的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆