Open link from Webview in a browser?

394 views
Skip to first unread message

YeShito

unread,
Nov 2, 2015, 10:07:12 PM11/2/15
to AndroidScript
Hi,
how can I open link from from Webview in a browser? I want to use it for notifications, there should be only small Webview at the bottom, with 2 lines of text or link. If they click on the link, it should open the URL in a browser.
thanks

Chris Hopkin

unread,
Nov 3, 2015, 9:05:40 AM11/3/15
to AndroidScript
Hi YeShito

You can call app.LoadUrl() from within your html page to open a Url in a new browser when you click a link. This is a method I've used before:

<html>
 
<head>
 
<title>Hello World!</title>
 
<script src='file:///android_asset/app.js'></script>
 
</head>
 
<body>
 
<p>Hello World!</p>
 
 
<a href="http://google.com">Open google.com in WebView</a>
 
 
<p></p>
 
 
<!-- id='extLink' identifies this link as one to open in a browser in document.onclick -->
 <a id="extLink" href="http://google.com">Open google.com in Browser</a>
 
 
<script>
 
   
// Override link clicks to open url in a browser
    document
.onclick = function(e) {        
       
if(e.target.id === "extLink")
       
{
            app
.OpenUrl(e.target.href);
           
           
// return false to prevent default action
           
// and stop event propagation
           
return false;
       
}
       
       
return true;
   
};
 
 
</script>
 
</body>
</html>

Dave Smart

unread,
Nov 3, 2015, 9:19:10 AM11/3/15
to AndroidScript
Of course you don't need to use a WebView to create a link. You could just do it like we do in the DroidScript About box by using a TextView control like this:-


    //Create forum link.
    var txtForum = app.CreateText( "" );
    txtForum.SetMargins( 0, 0.04, 0,0 );
    txtForum.SetTextSize( 16 );
    txtForum.SetTextColor("#56AEF2");
    txtForum.SetHtml( "<u>Discussion Group</u>" );
    txtForum.SetOnTouchDown( txtForum_OnTouchDown );
    layAbout.AddChild( txtForum );


//Launch forum link,
function txtForum_OnTouchDown()
{
}

YeShito

unread,
Nov 3, 2015, 9:32:09 AM11/3/15
to AndroidScript
Hi, thanks for the answers. I have used the second method many times with Text control, but now I need the method with the web page, so I can change the text and the link anytime. I didnt know how to use the function OpenURL from web page, it looks like this is exactly what I need.
Reply all
Reply to author
Forward
0 new messages