woolm110
woolm110

Reputation: 1204

window.open not working on Android Phonegap

I've been searching the internet for hours and can't seen to find a fix for my issue.

I've created a simple Phonegap app and now I want to link out to an external webpage. I'm running Cordova 3.0 and here's a list of my plugins installed:

org.apache.cordova.core.inappbrowser 0.2.0 "InAppBrowser"
org.apache.cordova.core.splashscreen 0.2.0 "Splashscreen"
org.apache.cordova.device 0.2.3 "Device"
org.apache.cordova.inappbrowser 0.5.0 "InAppBrowser"

and here's how I'm trying to open a link:

<a onclick="var ref = window.open('http://www.google.co.uk', '_system', 'location=yes');" id="external-link">Link</a>

All works completely fine on iOS but on Android nothing happens. I've checked logcat and I get no errors, no feedback, it just doesn't work.

I've also tried navigator.app.loadUrl but I then get an error that says "cannot load url of undefined"

I was really hoping this would be a simple thing to do but so far it's driving me crazy, anyone got any suggestions as to what I'm doing wrong?

Just for reference:

cordova_plugins.js

cordova.define('cordova/plugin_list', function(require, exports, module) {
module.exports = [
    {
        "file": "plugins/org.apache.cordova.core.splashscreen/www/splashscreen.js",
        "id": "org.apache.cordova.core.splashscreen.SplashScreen",
        "clobbers": [
            "navigator.splashscreen"
        ]
    },
    {
        "file": "plugins/org.apache.cordova.core.inappbrowser/www/InAppBrowser.js",
        "id": "org.apache.cordova.core.inappbrowser.InAppBrowser",
        "clobbers": [
            "window.open"
        ]
    },
    {
        "file": "plugins/org.apache.cordova.device/www/device.js",
        "id": "org.apache.cordova.device.device",
        "clobbers": [
            "device"
        ]
    },
    {
        "file": "plugins/org.apache.cordova.inappbrowser/www/inappbrowser.js",
        "id": "org.apache.cordova.inappbrowser.inappbrowser",
        "clobbers": [
            "window.open"
        ]
    },
]
});

config.xml

<widget xmlns     = "http://www.w3.org/ns/widgets"
        id        = "io.cordova.helloCordova"
        version   = "2.0.0">
    <name>Hello Cordova</name>

    <description>
        A sample Apache Cordova application that responds to the deviceready event.
    </description>

    <author href="http://cordova.io" email="[email protected]">
        Apache Cordova Team
    </author>

    <access origin="*"/>

    <content src="index.html" />

    <preference name="loglevel" value="DEBUG" />
    <!--
      <preference name="splashscreen" value="resourceName" />
      <preference name="backgroundColor" value="0xFFF" />
      <preference name="loadUrlTimeoutValue" value="20000" />
      <preference name="InAppBrowserStorageEnabled" value="true" />
      <preference name="disallowOverscroll" value="true" />
    -->

    <feature name="App">
      <param name="android-package" value="org.apache.cordova.App"/>
    </feature>
    <feature name="Geolocation">
      <param name="android-package" value="org.apache.cordova.GeoBroker"/>
    </feature>
    <feature name="Device">
      <param name="android-package" value="org.apache.cordova.Device"/>
    </feature>
    <feature name="Accelerometer">
      <param name="android-package" value="org.apache.cordova.AccelListener"/>
    </feature>
    <feature name="Compass">
      <param name="android-package" value="org.apache.cordova.CompassListener"/>
    </feature>
    <feature name="Media">
      <param name="android-package" value="org.apache.cordova.AudioHandler"/>
    </feature>
    <feature name="Camera">
      <param name="android-package" value="org.apache.cordova.CameraLauncher"/>
    </feature>
    <feature name="Contacts">
      <param name="android-package" value="org.apache.cordova.ContactManager"/>
    </feature>
    <feature name="File">
      <param name="android-package" value="org.apache.cordova.FileUtils"/>
    </feature>
    <feature name="NetworkStatus">
      <param name="android-package" value="org.apache.cordova.NetworkManager"/>
    </feature>
    <feature name="Notification">
      <param name="android-package" value="org.apache.cordova.Notification"/>
    </feature>
    <feature name="Storage">
      <param name="android-package" value="org.apache.cordova.Storage"/>
    </feature>
    <feature name="FileTransfer">
      <param name="android-package" value="org.apache.cordova.FileTransfer"/>
    </feature>
    <feature name="Capture">
      <param name="android-package" value="org.apache.cordova.Capture"/>
    </feature>
    <feature name="Battery">
      <param name="android-package" value="org.apache.cordova.BatteryListener"/>
    </feature>
    <feature name="SplashScreen">
      <param name="android-package" value="org.apache.cordova.SplashScreen"/>
    </feature>
    <feature name="Echo">
      <param name="android-package" value="org.apache.cordova.Echo"/>
    </feature>
    <feature name="Globalization">
      <param name="android-package" value="org.apache.cordova.Globalization"/>
    </feature>
    <feature name="InAppBrowser">
      <param name="android-package" value="org.apache.cordova.InAppBrowser"/>
    </feature>
    <!-- Deprecated plugins element. Remove in 3.0 -->
    <plugins>
    </plugins>
</widget>

I've also tried to update/re-add the InAppBrowser plugin from the CLI but I get an error that says it's not supported with my version of Cordova and that I need version 3.0, I ran sudo npm update -g cordova but kept getting the same error afterwards.

Any help would be greatly appreciated!

Thanks

Upvotes: 1

Views: 4904

Answers (2)

HischT
HischT

Reputation: 953

What helped me (PhoneGap 3.2) is adding the following to your config.xml file:

<gap:plugin name="org.apache.cordova.inappbrowser" />

This should be added within the widget tag.

Upvotes: 2

lulu88
lulu88

Reputation: 1714

Try adding this:

  http://docs.google.com/viewer?url=

to the front of the url you are trying to open.

So it will be something like this:

  if ( device.platform == 'android' || device.platform == 'Android') ) {
       url = "http://docs.google.com/viewer?url= "+ url;
  }

Upvotes: 0

Related Questions