Search code examples
javascripthtmlcordovainappbrowser

Check if inappbrowser open or closed


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?


Solution

  • 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())