admin管理员组

文章数量:1431420

In my cordova app I am opening(using InAppBrowser plugin) a webpage in inappbrowser by the following code.

var ref = cordova.InAppBrowser.open('', '_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?

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', '_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?

Share Improve this question asked Oct 5, 2017 at 14:28 jafarbtechjafarbtech 7,0251 gold badge41 silver badges59 bronze badges
Add a ment  | 

2 Answers 2

Reset to default 4

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');

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())
var browserRef = cordova.InAppBrowser.open('http://apache', '_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.

https://cordova.apache/docs/en/latest/reference/cordova-plugin-inappbrowser/#inappbrowseraddeventlistener

本文标签: javascriptCheck if inappbrowser open or closedStack Overflow