117
<a target="_blank" data-rel="external" href="http://www.kidzout.com">www.kidzout.com</a>

hey experts i am using phonegap 2.9.0 and i am using the above code to open the link in the browser but it opens it in the same app...... how to open it safari browser?

it opens the website in the same app and then i am unable to come back to the app, so i need to delete the app and install that again.....

ahsan ali
  • 1,173
  • 2
  • 8
  • 6
  • If you want to open `_blank` to external browser and `_self` to WebView, check my 2015 solution for Cordova 5.1.1: http://stackoverflow.com/a/32227524/82609 – Sebastien Lorber Aug 28 '15 at 14:07
  • See also the answers in a similar question: https://stackoverflow.com/a/26176013/1480587 and https://stackoverflow.com/a/46619378/1480587 – Peter T. Oct 07 '17 at 10:54

12 Answers12

227

As suggested in a similar question, use JavaScript to call window.open with the target argument set to _system, as per the InAppBrowser documentation:

<a href="#" onclick="window.open('http://www.kidzout.com', '_system'); return false;">www.kidzout.com</a>

This should work, though a better and more flexible solution would be to intercept all links' click events, and call window.open with arguments read from the link's attributes.

Remember you must install the InAppBrowser plugin for this to work:

cordova plugin add cordova-plugin-inappbrowser
Erti-Chris Eelmaa
  • 25,338
  • 6
  • 61
  • 78
freejosh
  • 11,263
  • 4
  • 33
  • 47
  • 3
    yes, i too used the same target="_blank" in the cordova 1.7.0 but now i am working with 2.9.0 and it is annoying me, your sujjestion as well not worked......:( – ahsan ali Jul 26 '13 at 17:41
  • 6
    yes i tried that too and followed the links too that you shared, thanks for your help but the problem is still there, the site is still opening in the app......:( – ahsan ali Jul 26 '13 at 19:56
  • hey ahsan, would you mind elaborating on what your issue was? I think I may be having the same problem. – whossname Sep 27 '13 at 05:29
  • 3
    Remember you must install the InAppBrowser plugin for this to work, instructions here: http://cordova.apache.org/docs/en/3.0.0/cordova_inappbrowser_inappbrowser.md.html#InAppBrowser – jackocnr Oct 07 '13 at 23:35
  • 7
    I have the same problem.. the solution is not working :( it opens just as a regular link (on android) – Daniel Nov 08 '13 at 13:53
  • I've got an `!!! FAILED BINDER TRANSACTION !!!` error (trying to open Base64 png image in browser (not in PhoneGap engine) – lukaszkups Dec 06 '13 at 12:37
  • It opens a new url... but doesn't open in Safari... no back button and has no way to go back to app. – Nick Song Aug 12 '14 at 02:59
  • Its important to add "return false;" after window open. – m1crdy Oct 22 '14 at 06:30
  • hence: it doesn't work when you use you need to use a – BonifatiusK Jan 13 '15 at 09:57
  • Works perfectly for me. using angularjs as well. –  Mar 11 '15 at 14:25
  • Nearly works for me, but only when modified: I need "_blank" instead of "_system" (on the Intel XDK emulator, at least). I'm on v0.6.0 of the Cordova InAppBrowser (yes, I know it's old, but I can't make the latest one work with XDK). – xgretsch Nov 24 '15 at 07:44
  • I got the following error when trying to add the plugin: "Error: Registry returned 404 for GET on https://registry.npmjs.org/org.apache.cordova.inappbrowser" I used the command mentioned here: http://cordova.apache.org/docs/en/3.0.0/cordova/inappbrowser/inappbrowser.html – Luke Wenke Sep 07 '16 at 01:10
  • cordova plugin add cordova-plugin-inappbrowser (new installation command) – Kaspi Sep 14 '16 at 12:04
  • I'm finding that the above code doesn't work in the latest cordova - but a slight adjustment and it does work. www.kidzout.com – Michael Mar 03 '17 at 03:21
31

As answered in other posts, you have two different options for different platforms. What I do is:

document.addEventListener('deviceready', onDeviceReady, false);

function onDeviceReady() {

    // Mock device.platform property if not available
    if (!window.device) {
        window.device = { platform: 'Browser' };
    }

    handleExternalURLs();
}

function handleExternalURLs() {
    // Handle click events for all external URLs
    if (device.platform.toUpperCase() === 'ANDROID') {
        $(document).on('click', 'a[href^="http"]', function (e) {
            var url = $(this).attr('href');
            navigator.app.loadUrl(url, { openExternal: true });
            e.preventDefault();
        });
    }
    else if (device.platform.toUpperCase() === 'IOS') {
        $(document).on('click', 'a[href^="http"]', function (e) {
            var url = $(this).attr('href');
            window.open(url, '_system');
            e.preventDefault();
        });
    }
    else {
        // Leave standard behaviour
    }
}

So as you can see I am checking the device platform and depending on that I am using a different method. In case of a standard browser, I leave standard behaviour. From now on the solution will work fine on Android, iOS and in a browser, while HTML page won't be changed, so that it can have URLs represented as standard anchor

<a href="http://stackoverflow.com">

The solution requires InAppBrowser and Device plugins

krzychu
  • 3,577
  • 2
  • 27
  • 29
  • Excellent answer. Just in case it doesn't for others, make sure your www/cordova_plugins.js has both the device and the inappbrowser configs included. When I install plugins, it adds the js & plugins configs to the staging folder and not my primary working www folder. Once I sorted out the config and locations, this worked perfectly. – Michael Bordash Nov 09 '14 at 04:50
  • 1
    This is the best answer! Please remember you have no install the InAppBrowser plugin via: ```$ meteor add cordova:org.apache.cordova.inappbrowser@0.5.4``` – tixastronauta Nov 18 '15 at 23:13
  • If anyone would like to contribute to the snippet, I uploaded it to gist. https://gist.github.com/redolent/e79722b32a48a536b5ba – redolent Nov 25 '15 at 18:43
30
<a onclick="navigator.app.loadUrl('https://google.com/', { openExternal:true });">Link</a>

Works for me with android & PG 3.0

user2758937
  • 327
  • 3
  • 3
21

There are 2 different ways to open URL in android and iphone.

FOR IOS use following code.

window.open("http://google.com", '_system');

and for android OS use following code.

navigator.app.loadUrl("http://google.com", {openExternal : true});
Hassan Siddique
  • 1,590
  • 14
  • 27
  • Cordova can use ``window.open("http://google.com", '_system')`` just as well. You shouldn't use ``navigator.app.loadUrl``, because it won't work with ``market://`` URLs: in that case, it will just close your app and open in the same window.. not always preferable. – Agamemnus Oct 06 '14 at 18:55
  • 1
    Agamemnus, window.open is not working on android in my phonegap 3.6 application. I need to use the solution in this answer, to make it work. – Moulde Jan 09 '15 at 09:46
13

None of these answers are explicit enough to get external links to open in each platform. As per the inAppBrowser docs:

Install

cordova plugin add cordova-plugin-inappbrowser

Overwrite window.open (optional, but recommended for simplicity)

window.open = cordova.InAppBrowser.open;

If you don't overwrite window.open, you will be using the native window.open function, and can't expect to get the same results cross-platform.

Use it to open links in default browser

window.open(your_href_value, '_system');

Note that the target for the inAppBrowser (which is what the plugin name suggests it is to be used for) is '_blank', instead of '_system'.


Without the steps above, I was not able to get links to open in the default browser app cross-platform.

Extra credit

Here's an example (live) click handler for the links:

document.addEventListener('click', function (e) {
    if (e.target.tagName === 'A' &&
        e.target.href.match(/^https?:\/\//)) {
        e.preventDefault();
        window.open(e.target.href, '_system');
    }
});
bozdoz
  • 12,550
  • 7
  • 67
  • 96
  • Where exactly do you override the window.open? In the website or the cordova app – Ellisan Jan 25 '18 at 14:37
  • Preferably the top of your JavaScript file that you load into your index.html page (your website, if that helps) – bozdoz Jan 25 '18 at 22:38
11

At last this post helps me on iOS: http://www.excellentwebworld.com/phonegap-open-a-link-in-safari-or-external-browser/.

Open "CDVwebviewDelegate.m" file and search "shouldStartLoadWithRequest", then add this code to the beginning of the function:

if([[NSString stringWithFormat:@"%@",request.URL] rangeOfString:@"file"].location== NSNotFound) {
    [[UIApplication sharedApplication] openURL:[request URL]];
    return NO;
}

While using navigator.app.loadUrl("http://google.com", {openExternal : true}); for Android is OK.

Via Cordova 3.3.0.

mytharcher
  • 742
  • 7
  • 18
  • This is the only thing that worked for me in 3.0.0. Thank you!! – Rocklan Mar 09 '14 at 06:09
  • In 3.0 you need to include the InAppBrowser plugin. However weird that may seem. – Sean Bannister Apr 19 '14 at 02:29
  • I would change it one layer above in MainViewController.m: - (BOOL) webView:(UIWebView*)theWebView shouldStartLoadWithRequest:(NSURLRequest*)request navigationType:(UIWebViewNavigationType)navigationType { NSURL *url = [request URL]; if ([[url scheme] isEqualToString:@"file"] || [[url scheme] isEqualToString:@"gap"]) { return [super webView:theWebView shouldStartLoadWithRequest:request navigationType:navigationType]; } else { [[UIApplication sharedApplication] openURL:url]; return NO; } } – Gamadril Sep 17 '15 at 13:36
7

If you happen to have jQuery around, you can intercept the click on the link like this:

$(document).on('click', 'a', function (event) {
    event.preventDefault();
    window.open($(this).attr('href'), '_system');
    return false;
});

This way you don't have to modify the links in the html, which can save a lot of time. I have set this up using a delegate, that's why you see it being tied to the document object, with the 'a' tag as the second argument. This way all 'a' tags will be handled, regardless of when they are added.

Ofcourse you still have to install the InAppBrowser plug-in:

cordova plugin add org.apache.cordova.inappbrowser
Michel Reij
  • 181
  • 1
  • 6
3
window.open('http://www.kidzout.com', '_system');

Will work but only if you have the inappbrowser plugin installed. To install, using terminal, browse to the www folder in your project and type:

phonegap plugin add org.apache.cordova.inappbrowser

or

cordova plugin add org.apache.cordova.inappbrowser

Then it your link will open in the browser.

Dev01
  • 13,292
  • 19
  • 70
  • 124
  • The command `phonegap local ` has been DEPRECATED. The command has been delegated to `phonegap `. The command `phonegap local ` will soon be removed. – Ajay Suwalka Dec 20 '14 at 13:31
  • I don't know in the past, but nowadays the correct to install this plugin is `cordova plugin add cordova-plugin-inappbrowser`. – Plinio FM Jun 16 '19 at 14:35
2

With Cordova 5.0 and greater the plugin InAppBrowser is renamed in the Cordova plugin registry, so you should install it using

cordova plugin add cordova-plugin-inappbrowser --save

Then use

<a href="#" onclick="window.open('http://www.kidzout.com', '_system');">www.kidzout.com</a>
codevision
  • 5,165
  • 38
  • 50
1

I'm using PhoneGap Build (v3.4.0), with focus on iOS, and I needed to have this entry in my config.xml for PhoneGap to recognize the InAppBrowser plug-in.

<gap:plugin name="org.apache.cordova.inappbrowser" />

After that, using window.open(url, target) should work as expected, as documented here.

contactmatt
  • 18,116
  • 40
  • 128
  • 186
  • 1
    I`m also using PhoneGap Build (v3.5.x) and added the plugin via the config.xml. But what i get is an InAppBrowser without controls an cannot call the safari browser. I`m just using the window.open(). Any advice? – Reinos Aug 22 '14 at 17:36
  • If it's setup correctly, window.open should just work. In my situation, window.open is opening another browser url, and it works okay. Make sure the URL is fine. – contactmatt Aug 22 '14 at 21:50
  • I've just tried this with Phonegap Build & It's erroring with: "plugin unsupported: org.apache.cordova.inappbrowser" – David Ball Mar 03 '20 at 14:48
1

I also faced the issue that link was not opening on browser here is my fix with steps:

1: Install this cordova plugin.

cordova plugin add cordova-plugin-inappbrowser

2: add the open link in the html like following.

<a href="#" onclick="window.open('https://www.google.com/', '_system', 'location=yes');" >Google</a>

3: this is the most importaint step due to this I faced lots of issue: download the cordova.js file and paste it in the www folder. Then make a reference of this in the index.html file.

<script src="cordova.js"></script>

This solution will work for both the environment android and iPhone.

Tabish
  • 1,592
  • 16
  • 13
-2

Like this :

<a href="#" onclick="window.open('https://www.nbatou.com', '_system'); return false;">https://www.nbatou.com</a>