Home > Uncategorized > Under the hood InAppBrowser for Cordova 3.5.0 (Phonegap)

Under the hood InAppBrowser for Cordova 3.5.0 (Phonegap)

In previous versions of Phonegap/Cordova (i.e. 2.x), the InAppBrowser was baked into Cordova.js proper, and was not considered a plugin, it was simply called via “Window.open”, and you got Phonegap’s ‘augmented’ version of the browser control.

However, after upgrading a project to Cordova 3.5.0, the inAppBrowser baffled me, since, although window.open was still working, it wasn’t using the inAppBrowser, so therefore, once window.open was called, the user had left the app, and was then on the web.

Anyway, I finally figured out that you needed to download the InAppBrowser as a plugin. Copy the js files into the src folder (talking about Android here), a couple of images for the forward and back button, and a js file into the www folder.

However that *bloody* js file didn’t work, not sure why, but calls to “require” and “modules”, were simply not there, so, since it was a short file, I decided to pick it apart, and just use the little functionality I required from it.

Now, I’m not advocating this approach, just if you can’t face it any longer, this hack may save you alot of time.

So, instead of Window.open(url, … ), I use a new function AltWindowOpen as follows:

function altWindowOpen(strUrl, strWindowName, strWindowFeatures)
{
console.error(“altWindowOpen()”);
var cb = function(event) {
console.error(“inappBrowser – ” + event.type);
if(event.type == “loadstart”)
{
browserRef_loadstart(event);
}
};
cordova.exec(cb, cb, “InAppBrowser”, “open”, [strUrl, strWindowName, strWindowFeatures]);
}

As you see, I’m only interested in the “loadstart” event, and I want to call browserRef_loadstart() whever that happens. The event object has the property “event.url” so you can see what it’s loaded.

In order to close the browser, you call

cordova.exec(null, null, “InAppBrowser”, “close”, []);

I’ve used console.error instead of console.log so it shows up in logcat more visibly. – Just another trick I discovered tonight.

Advertisements
Categories: Uncategorized
  1. davidtlee
    March 11, 2015 at 4:03 am

    Hi, I was wondering, do you know where I can find documentation on how inappbrowser calls cordova.exec? I was having difficulty getting inappbrowser to work, but it seemed to work when I call what you wrote above with cordova.exec. However, I need to bind an eventListener onto the browser window to listen for loadstop, and I’m not sure how to do that. Really appreciate your help!

  2. davidtlee
    March 11, 2015 at 4:04 am

    Hi, really appreciate your article. I was wondering, do you know how I can get the browser window that would normally be returned with window.open? I was originally trying to use inappbrowser’s window.open and having trouble. Your approach of directly calling cordova.exec seems to do it, but then I don’t get the returned window to attach eventListeners too. Thanks!

  1. No trackbacks yet.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: