Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I load a webpage inside the phonegap webview?

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?

like image 449
xdumaine Avatar asked Dec 21 '11 21:12

xdumaine


People also ask

How does Cordova WebView work?

You can think of the web view as a tab in a browser. When you compile a Cordova application, it doesn't actually take your HTML, CSS, and JavaScript code and automagically converts it into native code, specific to each platform. Cordova acts as a container for the app that you write using web technologies.

What is InApp browser?

The InAppBrowser is a web browser view that displays when calling [window. open](window. open. html)() , or when opening a link formed as <a target="_blank"> . var ref = window.


3 Answers

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

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

to the phonegap.xml file.

like image 56
xdumaine Avatar answered Oct 22 '22 06:10

xdumaine


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.

like image 28
dngrmice Avatar answered Oct 22 '22 07:10

dngrmice


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

like image 3
tapmonkey Avatar answered Oct 22 '22 07:10

tapmonkey