最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Check if inappbrowser open or closed - Stack Overflow

programmeradmin0浏览0评论

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

发布评论

评论列表(0)

  1. 暂无评论