Search code examples
javascriptandroidcordovaphonegap-pluginsphonegap-build

Using Cordova, how can I open external URLs in Chrome, instead of the in-app browser?


This question has been asked a few times before, but all of the solutions date back to 2013, and I haven't got any of those answers to work with the latest versions of PhoneGap Build / Cordova.

I have a link like this which I want to open in Chrome on Android.

<a href="https://twitter.com/humphreybc" target="_blank">Twitter</a>

In config.xml I have the following rules:

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

I have tried using window.open(url, _system) as suggested in other answers – and included the cordova-plugin-inappbrowser plugin – however:

  1. This solution doesn't seem to work, links just open in the in-app browser
  2. I'd rather simply use target="_blank" instead of using an onClick handler for each link

I've also followed the instructions in this blog and added a handler for links with the _target='blank' attribute:

$(document).on('click', 'a[target="_blank"]', function (e) {
  e.preventDefault();
  var url = this.href;
  window.open(url,"_system");                    
});

...but still the links open in the in-app browser.


Solution

  • Just going back through old questions and marking them as answered. I ended up doing this:

    function onDeviceReady() {
      return $(document).on('click', 'a[target="_blank"]', function(e) {
        e.preventDefault();
        return window.open(this.href, '_system');
      });
    };
    
    if (!!window.cordova) {
      document.addEventListener('deviceready', onDeviceReady, false);
    }