WebView - open links in default browser app?

1,416 views
Skip to first unread message

stamja

unread,
May 30, 2016, 9:02:45 PM5/30/16
to DroidScript
Hi all, I'm wondering if there is an option when creating a WebView to have links open up in the default browser app (i.e., rather than the linked page opening in the WebView).

Also, is there an exhaustive list somewhere of all WebView options?

Any help appreciated, thanks.

nikhil baby

unread,
May 30, 2016, 10:09:23 PM5/30/16
to DroidScript
you can do this by setting a a interval to the following code

var URL = "Your website URL"
var URL2 = webview.GetUrl();

if ( URL2==URL ){
}
else {
app.OpenUrl( URL2 );
}

John LaDuke

unread,
May 30, 2016, 10:35:02 PM5/30/16
to DroidScript
If you want to activate another app, use SendIntent.

stamja

unread,
May 31, 2016, 12:02:35 AM5/31/16
to DroidScript
I was hoping there might be an option I could pass to app.CreateWebView() that would simply alter its behaviour so that any hyperlink touched in the webview would open in the browser app, externally to the DS app.

Something like:
app.CreateWebView( 1.0, 0.5, "IgnoreErrors,NoScrollBars,UseBrowserForLinks" );

Anyway, many thanks for your suggestions. :)

BareK

unread,
May 31, 2016, 5:28:00 AM5/31/16
to DroidScript
Hi stamja,

I don't think Dave thought of such an option, as the original goal of the webview was to be a substitute of the default browser and to provide navigation.
However, as more and more users uses webviews for display only (like using Material Design), it could make sense and simplify this behaviour.

Thanks to nikhil baby (cauz I didn't knew app.LoadUrl), I wrote this sample code that should make the trick:

var loadComplete = false;
var defaultUrl   = "http://www.google.com";
var currentUrl   = null;


function OnStart()
{    
   
var lay = app.CreateLayout( "Linear", "VCenter" );
   
   
var web = app.CreateWebView( 1.0, 1.0, "IgnoreErrors,NoScrollBars" );
    web
.SetOnProgress( UseBrowserForLinks );
    lay
.AddChild( web );
   
    app
.AddLayout( lay );
   
    web
.LoadUrl( "http://www.google.com" );
}


function UseBrowserForLinks( progress )
{
   
// Treating the first URL
   
if( !loadComplete )
   
{
       
if( progress === 100 )
       
{
           
// Indicates the first URL is loaded
            loadComplete
= true;
           
           
// Saves the loaded URL as defaultURL (in case of redirection or sth)
            defaultUrl
= this.GetUrl();
       
}
   
}
   
   
// For the other URLs
   
else
   
{
       
var url = this.GetUrl();
       
       
// Ignore the defaultURL and the currentURL
       
if( url !== null && url !== defaultUrl && url !== currentUrl )
       
{
           
// Updates currentURL to not re-open it
            currentUrl
= url;
           
           
// Go back to the first URL (equals "this.LoadUrl( defaultUrl );" )
           
this.Back();
           
           
// Open the link's URL in default web browser
            app
.OpenUrl( currentUrl );
       
}
   
}
}


Hope it helps :)

stamja

unread,
May 31, 2016, 8:52:19 AM5/31/16
to DroidScript
Hi BareK

Many thanks for taking the trouble.

Not a bad solution, however the drawback is that the link url has already commenced loading before the webview.Back() function is called.

So the original page has to reload, if it exists, which causes the content to redraw.

If the original page was not a url but was created using webview.LoadHtml(), then the Back() function will have nowhere to go back to, unless your code re-loads the html. Again this will cause the original page content to redraw.

It would require some more refinement to make this work satisfactorily, but it could be the only solution to this issue as things stand. Unfortunately the page I want to use it with has a lot of active content, so reloading it looks far too 'messy'.

But thanks again. :)


BareK

unread,
May 31, 2016, 12:11:54 PM5/31/16
to DroidScript
I agree that it's not perfect, and actually there's no way to prevent the page to start loading.
It could be usefull to have a function like webview.SetBeforeLoad( cb ); and a mechanism for blocking the load of certain URL if we want :)

If your HTML is local (webview.LoadHtml) you could try to load it in a text:

function OnStart()
{
 
var lay = app.CreateLayout( "linear", "VCenter,FillXY" );


 
var txt = app.CreateText( "", -1, -1, "Html,Links" );
 txt
.SetTextSize( 32 );
 lay
.AddChild( txt );


 app
.AddLayout( lay );
 
 txt
.SetHtml( "<a href='http://www.google.com'>Test me :)</a>" );
}


But I'm not sure that it suits to your needs.

Dave Smart

unread,
May 31, 2016, 2:41:40 PM5/31/16
to DroidScript
Hi Stamja,

Do you have control over the web page?  If so, then you could use an onclick event in the web page and call the app.OpenUrl() method.  You will need to set the 'AllowRemote' option in the app.SetOptions() method and make sure that users can only view your own web site through the webview (for security reasons).

Failing that, then I suggest you add to the 'Tell us what you want' thread.

Regards
David

stamja

unread,
Jun 1, 2016, 8:51:15 AM6/1/16
to DroidScript
Hi BareK and Dave

Thanks for your replies.

Dave's suggestion of using onclick events would seem the best workaround for 'local' html pages, such as those loaded using webview.LoadHtml().

BareK's suggestion of webview.SetBeforeLoad(cb) is sort-of equivalent to using onclick events in the html, so I guess adding another method to webview would be re-inventing the wheel somewhat.

Apart from local html, I also see potential in using the webview to create a DS app as a wrapper for public pages, such as weather sites - for example, the Bureau of Meteorology here in Australia has a neat mobile site that lends itself to being used as a standalone app.

Anyway I'm not sure whether what I've suggested above is even feasible, and I suppose it's really quite moot as to what weight should be given to prioritise any work on this.

Cheers and thanks again. :)


Dave Smart

unread,
Jun 1, 2016, 9:05:28 AM6/1/16
to DroidScript
Hi Stamja,

Are you on Premium?

If so, then you can ask for that functionality and get it prioritized above non-premium users requests ;)

stamja

unread,
Jun 1, 2016, 9:17:17 PM6/1/16
to DroidScript
Thanks Dave, at this point I'm not able to devote so much time to DS to commit to Premium. However, will see in future.

Cheers. :)

Isaiah Nixon

unread,
Nov 1, 2017, 6:38:35 PM11/1/17
to DroidScript
Hey Dave, I have had the same problem recently as stamja had. I just wonder why adding an option for webView to use app.OpenUrl() for all links would be so difficult considering that this functionality is already built into the Text component.

Dave Smart

unread,
Nov 1, 2017, 7:53:50 PM11/1/17
to DroidScript
That's actually easy... I've just added it to the next beta version.  CreateWebView has a new option 'UseBrowser' which causes links to go external :)

Isaiah Nixon

unread,
Nov 2, 2017, 3:07:56 PM11/2/17
to DroidScript
Oh awesome! Do you know when that functionality will be available to use?
Reply all
Reply to author
Forward
0 new messages