phonegap: open external page and then go back to app

Marco Gagliardi picture Marco Gagliardi · Oct 17, 2012 · Viewed 13.8k times · Source

ok, browsing the questions I've found the right way to load an external page into the phonegap view (i.e. without loosing the session or opening the device browser) as explained here: How can I load a webpage inside the phonegap webview? and here: PhoneGap for iPhone: problem loading external URL

Next step is: after i've opened an extarnal page (it's owned by me and I can modify it) how can i go back to my local application? Let's say I have a link in the external page and I want the user to be redirected back to a local html page (mypage.html) inside the phonegap application on click.

What url should the link's href attribute have? I've tried setting it to "file:///android_asset/www/mypage.html" but didn't work

Answer

Simon MacDonald picture Simon MacDonald · Oct 17, 2012

You want to use the ChildBrowser plugin to open the external web page. Then you want to set the ChildBrowser.onLocationChange property to your own function. Then when the person navigates away from the remote page you will be notified of the location change so you can then close the ChildBrowser and navigate to a new local page. You won't even need to touch the remote html page.

So to close the browser when the user navigates away from the remote page:

cb.onLocationChange = function(loc){
    console.log("location = " + loc);
    if (loc != "http://example.com") {
        cb.close();
    }
};