0

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?

Littm
  • 4,923
  • 4
  • 30
  • 38
user818671
  • 389
  • 4
  • 8
  • 21

7 Answers7

1

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/*" />

jcesarmobile
  • 51,328
  • 11
  • 132
  • 176
0

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.

Littm
  • 4,923
  • 4
  • 30
  • 38
  • :( ... Did you get any error on console? Also, is the `alert("hi");` triggered if not commented ? – Littm Oct 15 '12 at 07:24
  • Do you get any error on console? I'll post a simple example using window.location.href... Then, you could try it to see if the problem comes from there ok? Please wait... – Littm Oct 15 '12 at 08:40
  • Please have a try with the example I've just posted :). I hope this will work for you :S – Littm Oct 15 '12 at 08:48
  • yes your example worked for me. But it does not work in my application. – user818671 Oct 15 '12 at 09:04
  • You may have an code issue somewhere :S. What you can do is to open your js file with your browser (ex: firefox) and check if you don't have any synthax error by using your console (or Firebug :) ). – Littm Oct 15 '12 at 09:20
0

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!

Chris Lambrou
  • 356
  • 2
  • 7
0

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?

Community
  • 1
  • 1
Federico
  • 6,388
  • 6
  • 35
  • 43
0

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

Thomas Decaux
  • 21,738
  • 2
  • 113
  • 124
0

Try without href:

window.location = "http://www.google.com";
Undo
  • 25,519
  • 37
  • 106
  • 129
shamaleyte
  • 1,882
  • 3
  • 21
  • 38
0

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);
}
Seven
  • 843
  • 8
  • 10