phonegap - open link in browser
Asked Answered
C

9

14

I use phonegap (cordova 2.2)

I have link like this :

<a href="http://twitter.com/foobar" target="_blank">twitter</a>

On iOS - it opens link in browser(Safari)

But on Android - it opens inside webview(inside my phonegap app)

Is there a way to make Android work same way as iOS ?

Comintern answered 28/12, 2012 at 14:17 Comment(0)
E
27

This is how I got it working using Cordova 2.2 and jQuery mobile on Android

Javascript:

$('.link').live('tap', function() {
    url = $(this).attr("rel");   
    loadURL(url);
});

function loadURL(url){
    navigator.app.loadUrl(url, { openExternal:true });
    return false;
} 

html:

<a href='#' class='link' rel='http://www.someurl.com'>Go Somewhere</a>
Exalt answered 31/12, 2012 at 4:38 Comment(2)
Great to know this.. this worked for me using Cordova 2.3, jQuery Mobile on Android.Conciseness
Live is deprecated. Hell, I thought it was removed in more recent versions of JQ. I'd go with using delegate or the on method but delegation style. Actually, so is delegate, but much more recently.Wivern
C
6

Try this for android:

function loadURL(url){
    navigator.app.loadUrl(url, { openExternal:true });
    return false;
} 

Html:

<a click="loadURL('http://twitter.com/foobar')">twitter</a>

You can also try this in your config.xml:

<access origin="*twitter.com" browserOnly="true"/> 
Cadent answered 28/12, 2012 at 14:24 Comment(1)
Worked for me with onclick instead of click. The property click doesn't exist, or does?Wedding
A
4

The link provided by user1879822 was actually the most useful one for me: https://build.phonegap.com/blog/access-tags

To summarize, PhoneGap has a whitelist of allowed URLs within its config.xml. This means if it has an entry like this ...

<access origin="*" />

... it will attempt to open all links inside its own webview. However if you constrain your whitelist to only specific URLs, then any link to a URL not in that list will automatically open in an external browser, not within your local webview. For example if you constrain it to only this ...

<access origin="http://127.0.0.1*" />

... then the twitter link mentioned in the original question should open in a new external browser.

Assignation answered 1/6, 2013 at 19:59 Comment(0)
M
3

If you want to use as in the ios version, with target="_blank" attributes:

$(document).on('tap', 'a[target="_blank"]', function(e){
    navigator.app.loadUrl(e.target.href, { openExternal: true });
    return false;
});
Mcclean answered 20/3, 2013 at 12:31 Comment(0)
F
1

I use this as a general rule:

$('a').live('tap',function(e){
    // if external link then open a browser
    if(String($(this).attr('href')).substring(0,4)=='http' || String($(this).attr('href')).substring(0,5)=='https'){
        navigator.app.loadUrl($(this).attr('href'), { openExternal:true });
        e.stopPropagation();
        return false;
    }
});
Fullblooded answered 22/2, 2013 at 7:43 Comment(2)
FYI, your https comparison is redundant, it will always evaluate to true in the first expression. :)Coriolanus
actually, it's meant to detect if it is an external link or not. but yeah, you're right. :)Fullblooded
N
1

I had the same exact problem and I noticed that most of the answers are mixed up for different platfoms. The solution works for me is Detail Explanation for different platforms

Noyes answered 11/4, 2013 at 5:22 Comment(1)
Please edit your answer so it provides all relevant text here and provide the link as more detailed backup information. That also keeps the answer useful even if that links becomes invalidBemis
T
1

Even if this question was asked a while ago I wanted to inform you about the following blod entry which helped me out:

https://build.phonegap.com/blog/access-tags

In android all I had to to was to unwhitelist my specified domain. So in my config.xml I do not have any ` at all.

Trinidadtrinitarian answered 10/6, 2013 at 12:23 Comment(0)
A
0

This worked for me on ios

                    $("a[target='_blank']").on('tap touch click',function(e){
                        e.stopPropagation();
                        e.preventDefault();
                        window.open($(this).attr('href'), "_system");
                        return false;
                    });
Alleged answered 15/5, 2013 at 9:26 Comment(0)
K
0

Navigator for phonegap works!

handler: function (btn, evt) {
loadURL('http://www.google.com');
}

...

function loadURL(url){
navigator.app.loadUrl(url, { openExternal:true });
return false;
} 
Knockwurst answered 27/11, 2013 at 17:10 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.