Search code examples
androidcordovaphonegapphonegap-cli

phonegap window.location=url not working


I'm totally new at Phonegap development and am trying something that seems rather simple but isn't working.

I followed the Phonegap "getting started" guides and have the "PhoneGap Developer" app installed on my Android phone. I created an application and am serving it with phonegap serve. I removed the default js/index.js and call to app.initialize() and have the following added to the index.html in its place:

<script>
    function onDeviceReady() {
        if (navigator.connection.type == Connection.NONE) {
            navigator.notification.alert(
              'An internet connection is required to continue');
        } else {
            navigator.notification.alert("HERE!");
            window.location = "https://google.com";
        }
    }
    document.addEventListener("deviceready", onDeviceReady, false);
</script>

If I access the app with a Chrome browser it properly loads, gives the popup message, and then loads up Google. If I try using the Android PhoneGap Developer app it pops up with the message and then does nothing.

In the config.xml I tried to set everything as permissive as possible just to get it to work:

<access origin="*" />
<allow-navigation href="*" />
<allow-intent href="*" />

On the server console I get the following warning:

[phonegap] [console.warn] Content Security Policy has been modified to be: <meta http-equiv="Content-Security-Policy" content="default-src * 'unsafe-inline' gap: ws: https://ssl.gstatic.com;style-src 'self' 'unsafe-inline' data: blob:;media-src *;img-src * 'self' data: content:;script-src * 'unsafe-inline' 'unsafe-eval' data: blob:;">

I'm not familiar enough with CSP policies to determine if that policy actually restricts loading 'google.com' or not...


Solution

  • Phonegap developer app is a Phonegap app itself, so it uses it's own config.xml instead of using yours. You can see on the github repo that it doesn't have any allow-navigation rules on it. So it won't allow to navigate to google or any other site.

    What you have to do it to run your own app instead of "preview" it on the Phonegap developer app.

    This is the command to run your app phonegap run android

    Also, you might need to install the whitelist plugin with phonegap plugin add cordova-plugin-whitelist or adding <plugin name="cordova-plugin-whitelist" spec="~1.3.2" /> to your config.xml, as the Phonegap Developer app comes with all the core plugins installed, but your app might not have it deppending on the template.