Reputation: 7013
In my cordova app I am opening(using InAppBrowser
plugin) a webpage in inappbrowser by the following code.
var ref = cordova.InAppBrowser.open('http://apache.org', '_blank', 'location=yes');
ref.close();
Now I want to check if the InAppBrowser is closed(not a particular URL I want to check the browser itself open or closed) in js like ref.isClosed()
. But InAppBrowser doesn't facilitate such function. Is there any way to find it?
Upvotes: 2
Views: 2945
Reputation: 2569
var browserRef = cordova.InAppBrowser.open('http://apache.org', '_blank', 'location=yes');
var isOpen = true;
browserRef.addEventListener('exit', function(event) {
console.log('exit: event fires when the InAppBrowser window is closed.');
isOpen = false;
});
Add a listener for an exit event from the InAppBrowser. This will allow you to perform logic or set a variable to manage state.
Upvotes: 3
Reputation: 30366
You could just create a simple wrapper to keep track of whether it's open or closed, e.g.:
var iab_controller = (function(){
var ref, isOpen = false;
return {
open: function(url){
ref = cordova.InAppBrowser.open(url, '_blank', 'location=yes');
isOpen = true;
return ref;
},
close: function(){
if(isOpen){
ref.close();
ref = null;
isOpen = false;
}
},
isOpen: function(){
return isOpen;
},
isClosed: function(){
return !isOpen;
}
};
})();
console.log("isOpen: " + iab_controller.isOpen())
console.log("isClosed: " + iab_controller.isClosed())
iab_controller.open('http://apache.org');
console.log("isOpen: " + iab_controller.isOpen())
console.log("isClosed: " + iab_controller.isClosed())
iab_controller.close();
console.log("isOpen: " + iab_controller.isOpen())
console.log("isClosed: " + iab_controller.isClosed())
Upvotes: 4