1

I use the Phonegap Childbrowser for oAuth 2.0 authentication. After the authentication, I want to use Javascript in the web page to hide the Childbrowser without pressing the "done" button. How to do that?

For example, after authentication, we always get some callback parameters. That means our authentication is successful. At this time, we should close our browser, and I want to close the browser by Javascript without pressing any buttons.

dda
  • 6,030
  • 2
  • 25
  • 34
CashLee李秉骏
  • 1,038
  • 1
  • 10
  • 23

2 Answers2

2

Capture onLocationChange on childBrowser and check the url parameter to see if it contains what you're looking for. If so, call childBrowser.close().

One weirdness of ChildBrowser is that it doesn't have a .clear() method, so I've created a page called nothing.html that is blank. If you don't, you'll briefly see your auth page the next time you use ChildBrowser. This doubles as my redirectUrl for Facebook and Twitter auth. So when it gets to that page, I know it's time to close.

var URL_NOTHING = 'nothing.html';
window.plugins.childBrowser.onLocationChange = function ( url ) {
    if ( url.indexOf( URL_NOTHING ) > -1 ) {
        if ( url.indexOf( 'oauth_token' ) > -1 ) { //twitter
            window.plugins.childBrowser.close();
            //do twittery stuff
        } else { //facebook
            window.plugins.childBrowser.close();
            //do facebooky stuff
        };
    };
};
ThinkingStiff
  • 64,767
  • 30
  • 146
  • 239
0

ChildBrowser plugin has a close() method to close the window from javascript code.

window.plugins.childBrowser.close();
dhaval
  • 7,611
  • 3
  • 29
  • 38