打开 InAppBrowser 的两个实例(_system 和 _blank)可防止事件触发
Opening two instances of InAppBrowser (_system and _blank) prevents events from triggering
我们目前正在使用 cordova 和 InAppBrowser 插件开发应用程序。我们正在尝试同时生成两个不同的 IAB 实例。一个带有 _system 浏览器,另一个带有 _blank 选项。
我们遇到的问题是,一旦我们打开 _system 浏览器的实例,我们似乎就失去了对以前浏览器的引用。因此,关闭事件不会在 _system 浏览器关闭后在 _blank IAB 上触发。
这就是实际代码的样子。
// 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);
}
有人知道发生了什么事吗?
似乎每次执行新 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;
};
我们目前正在使用 cordova 和 InAppBrowser 插件开发应用程序。我们正在尝试同时生成两个不同的 IAB 实例。一个带有 _system 浏览器,另一个带有 _blank 选项。
我们遇到的问题是,一旦我们打开 _system 浏览器的实例,我们似乎就失去了对以前浏览器的引用。因此,关闭事件不会在 _system 浏览器关闭后在 _blank IAB 上触发。
这就是实际代码的样子。
// 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);
}
有人知道发生了什么事吗?
似乎每次执行新 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;
};