Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to close InAppBrowser itself in Phonegap Application?

I am developing Phonegap application and currently i am using InAppBrowser to display external pages. On some of the external pages I place a close button and i want to close the InAppBrowser itself. because InAppBrowser displays these pages that is why the reference of it is not accessed on itself to close it and Please do not suggest me to use ChildBrowser Plugin.

window.close(); //Not Worked for me
or  
iabRef.close();  //Also not Worked for me because iabRef is not accessible on InAppBrowser. It is created on Parent Window

Some of the Android device and iOS device display a Done Button to close it. As well as the iPad also display the Done button. but in Case of Android tablet there is not any kind of button to close it.

UPDATE :-

Here is my full code :-

var iabRef = null; 
function iabLoadStart(event) {
}
function iabLoadStop(event) {
}
function iabClose(event) { 
    iabRef.removeEventListener('loadstart', iabLoadStart);      
    iabRef.removeEventListener('loadstop', iabLoadStop); 
    iabRef.removeEventListener('exit', iabClose); 
}
function startInAppB() {
    var myURL=encodeURI('http://www.domain.com/some_path/mypage.html');
    iabRef = window.open(myURL,'_blank', 'location=yes');    
    iabRef.addEventListener('loadstart', iabLoadStart);
    iabRef.addEventListener('loadstop', iabLoadStop);
    iabRef.addEventListener('exit', iabClose);
}
like image 969
Shashi Avatar asked Jan 13 '14 06:01

Shashi


People also ask

How do I close InAppBrowser?

Using the loadstart event to close the InAppBrowser Add a loadstart event listener to listen to URL loading events. Create a loadStartCallBack callback to check the loaded URLs. If the loaded URL matches the predefined close URL, close the IAB window.

What is InAppBrowser?

The InAppBrowser is a web browser view that displays when calling [window. open](window. open. html)() , or when opening a link formed as <a target="_blank"> . var ref = window.

What is Cordova InAppBrowser?

We can define the InAppBrowser as a native Cordova plugin that mainly used to add an in-app browser for any of the hybrid mobile applications. By using the InAppBrowser, we can open an external link from the Cordova application. It can easily load a webpage inside the Cordova app.


2 Answers

This worked for me:

var win=window.open( "myurl", "_blank");
win.addEventListener( "loadstop", function(){
       var loop = window.setInterval(function(){
           win.executeScript({
                   code: "window.shouldClose"
               },
               function(values){
                   if(values[0]){
                     win.close();
                     window.clearInterval(loop);
                   }
               }
           );
       },100);
   });

In your called window, simply do:

window.shouldClose=true

When you want to close it

like image 111
Torsten Simon Avatar answered Nov 03 '22 00:11

Torsten Simon


There's a solution provided in this blog post: cross window communication with cordova's inappbrowser

Essentially, you could use executeScript() from the parent window to the InAppBrowser instance over and over (once or twice a second, for example), to check whether a value in the IAB has been set. Pressing the "close" button in IAB could set such a variable.

When the parent window discovers that the variable has been set in the IAB, the parent window uses the IAB reference to close() it.

like image 40
jokeyrhyme Avatar answered Nov 03 '22 01:11

jokeyrhyme