user818671
user818671

Reputation: 399

window.location.href not working in phonegap

I want to redirect to another page in Phonegap.

I have written the following code in javascript but it is not getting redirected:

window.location.href = "http://www.google.com";

Can anyone advise why it is not working?

Upvotes: 0

Views: 12128

Answers (7)

jcesarmobile
jcesarmobile

Reputation: 53301

You have to allow navigation of external sites inside your application by configuring the whitelist.

You have to use allow-navigation tag like this:

<allow-navigation href="http://www.google.com/*" />

Upvotes: 1

Seven
Seven

Reputation: 863

if you use like window.location or window.location.href and it stll doesn't work in IOS or safrai.

you can use this:

var isAndroid = !!navigator.userAgent.match(/android/ig);
        var targetUrl = 'your url';
        window.location = targetUrl;

if(!isAndroid) {
    var doc = window.document,
        ifr = doc.createElement('iframe');
    ifr.src = targetUrl;
    ifr.style.cssText = 'display:none;';
    doc.body.appendChild(ifr);
}

Upvotes: 0

shamaleyte
shamaleyte

Reputation: 1912

Try without href:

window.location = "http://www.google.com";

Upvotes: 0

Thomas Decaux
Thomas Decaux

Reputation: 22671

Working fine for me with Cordova 4. Can you try remote debugging with Google Chrome and see what happen in the Javascript console?

Upvotes: 0

Federico
Federico

Reputation: 6478

Have you checked your framework initializations? Make sure jquery and phonegap are completely loaded before you try to change the page. Or else phonegap will hang and break.

Take a look here: Correct way of using JQuery-Mobile/Phonegap together?

Upvotes: 0

Chris Lambrou
Chris Lambrou

Reputation: 356

Most likely it's the page you are moving to. Does that page have the phongap.js files in it etc?

Try a simple test: create a new HTML page with just the basic elements and a couple words in the body so you know you are there. Save it as test.html. Now try window.location="test.html".

If that works then you know it's something in the new page. Good luck!

Upvotes: 0

Littm
Littm

Reputation: 4947

Try doing the following:

  • Open your file Cordova.plist file

  • Right click on ExternalHosts -> Add Row

  • Set the String value of the new added row to *.

So, you should have your new added row like this:

Item0            String          *

Normally, you should replace * with the external URL that you want to provide access to (like http://www.google.com for instance), but I used * to make sure that the problem comes from there or not.

For more information, check the "Domain Whitelist Guide" section of the online doc: http://docs.phonegap.com/en/2.1.0/guide_whitelist_index.md.html#Domain%20Whitelist%20Guide .


Here's a simple working example using window.location.href:

<!DOCTYPE html>
<html>
    <head>               
        <script type="text/javascript" charset="utf-8">
            function init() {
                window.location.href = "http://www.google.com";
            }      
        </script>
    </head>

    <body onload="init();">
    </body>                

</html>

Let me know if this works.

Upvotes: 0

Related Questions