How can I load a webpage inside the phonegap webview?
Asked Answered
M

3

14

I want to be able to link to a webpage inside the phonegap webview that loads an external webpage inside the same phonegap webview. If I do this, it loads inside the webview:

public class App extends DroidGap {
   @Override
   public void onCreate (Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      super.loadUrl("http://google.com");
   }
}

However, I want to have an internal page launched first, with a link to the external page, so I do this:

public class App extends DroidGap {
   @Override
   public void onCreate (Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      super.loadUrl("file:///android_asset/www/index.html");
   }
}

and I have a link:

<a href="#" onclick="navigator.app.loadUrl('http://google.com')">Google</a>

but this link launches google outside the app, in the web browser, instead of in the phonegap webview. What can I do to make sure the link to the external page is launched inside the app's phonegap webview?

Mohamedmohammad answered 21/12, 2011 at 21:42 Comment(1)
Where do I add this?Greathearted
M
18

Ahhh.. Found the answer on this question. I had to add

<access origin="www.google.com"/>

to the phonegap.xml file.

Mohamedmohammad answered 21/12, 2011 at 21:52 Comment(0)
B
10

This seems to have changed, and access origin seems to have no effect. If you're using the cordova whitelist plugin, as seems to be standard. You need to use allow-navigation in your config.xml file. Without this it will open your web browser.

<plugin name="cordova-plugin-whitelist" version="1"/>
<allow-navigation href="https://google.com/*" />

Then you can use window.location = 'https://google.com' to move to another webpage within your JS.

Boatsman answered 27/4, 2015 at 20:22 Comment(4)
This should be the correct answer. Saved me a lot of grief. The other answers are outdated for v4. I just added <access origin="*" />Tiptop
I do not think that the <plugin> tag is necessary. Just the <allow-navigation>.Loriannlorianna
I tried and it did not seem to work for me with CLI 5.1.1. On Android it always open in a new tab :( even with <allow-navigation href="*" /> and the plugin installedKatheryn
@SebastienLorber did you found any alternative? i am struggling with the same problem.Tenorite
C
3

In the latest phonegab (1.7) in Cordova.plist there is a Key: OpenAllWhitelistURLsInWebView set this to YES.

Cupronickel answered 5/5, 2012 at 23:9 Comment(1)
..sorry, this is obviously only for iOS.Cupronickel

© 2022 - 2024 — McMap. All rights reserved.