whynotmatt
whynotmatt

Reputation: 61

Phonegap window.open does not work on Android

I have an iOS/Android app built on cordova 2.6 and jqm 1.3. I need to open a link to an external website after the user clicks on a button. The code I am using is:

var ref = window.open('http://google.com','_self','location=yes');
ref.addEventListener('loadstart',function(event) {
    console.log('load started');
});
ref.addEventListener('loadstop',function(event) {    
    console.log('load stopped');
});
ref.addEventListener('loaderror',function(event) {
    console.log('load error = ' + JSON.stringify(event));
});

On iOS everything performs like I would expect. A new browser window opens with the google website loaded. But I cannot get anything to to load in Android. When I click on the button, nothing happens. I have put in console statements before and after the window.open, so I know the code is at least being executed.

My config.xml should be wide open for white listed sites:

<access origin=".*"/>;

I have tested on a Nexus 7 (android 4.2) and an android 2.2 emulator with the same results on both.

Does anyone know why window.open would not be firing correctly on android?

Upvotes: 2

Views: 3295

Answers (2)

whynotmatt
whynotmatt

Reputation: 61

It looked like it was a problem with 2.6 loading plugins on Android. I upgraded to 2.7 and everything started to work.

Upvotes: 1

Juli&#235;n
Juli&#235;n

Reputation: 9532

Perhaps it's a solution to use the ChildBrowser plugin? This gives you a bit more control over the operation itself, while still preserving platform compatibility between iOS and Android.

In most cases, I use something like the following snippet to use the childbrowser to display an external page.

function openBrowser(url) {

    // determine if the childbrowser plugin is available
    var useChildBrowser = ('plugins' in window && window.plugins.childBrowser);

    if (useChildBrowser) {
        popup = window.plugins.childBrowser;
        popup.showWebPage(url, { showLocationBar: false, showAddress: false });
    } else {
        popup = window.open(url, 'Share', "['width=600px', 'height=400px', 'resizable=0', 'fullscreen=yes']"); 
    }
}

Note that this falls back to using window.open if the ChildBrowser plugin isn't available, so you won't break anything else with this. Could be worth a shot, perhaps?

Upvotes: 0

Related Questions