Webview callbacks

254 views
Skip to first unread message

PedroD

unread,
Oct 26, 2015, 7:55:15 AM10/26/15
to AndroidScript
I want to action a callback from a button in a webview, what is wrong with this trivial example?


function OnStart()

{

    lay = app.CreateLayout("linear", "VCenter,FillXY");    

    web = app.CreateWebView(0.8, 0.8);

//I want to call a DS function
    web.LoadHtml("<button onclick=boo()>say boo</button>");

    lay.AddChild(web);

    app.AddLayout(lay);

}

  
function boo() { app.ShowPopup("Boo!"); };

Pete

Steve Garman

unread,
Oct 26, 2015, 8:20:50 AM10/26/15
to AndroidScript
Two things.

1 Your HTML does not know anything about DroidScript

2 Even if it did, onclick would not know how to find a function outside the WebView.

Try this

function OnStart()

{
    var s='<script src="file:///android_asset/app.js"></script>'+
    '<script type="text/javascript">'+
    ' '+
    'function myboo(id,field,deflt){ '+
    'app.Execute("boo()")}</script>'
    lay = app.CreateLayout("linear", "VCenter,FillXY");    

    web = app.CreateWebView(0.8, 0.8);

    //I want to call a DS function
    web.LoadHtml(s + "<button onclick=myboo()>say boo</button>");

PedroD

unread,
Oct 26, 2015, 12:29:46 PM10/26/15
to AndroidScript
Thanks Steve,

It was obvious to me that the snippet would not work,but there is nothing in the wiki exploring how to do webview callbacks,

Regards

Pete

Steve Garman

unread,
Oct 26, 2015, 10:17:19 PM10/26/15
to AndroidScript
Pete,
if you feel like adding a sample and/or a description to the wiki, it would be much appreciated.

If you (or anyone else) want to just clean up that snippet with a bit of an explanation, it would be better than nothing.

John LaDuke

unread,
May 27, 2016, 8:21:30 AM5/27/16
to DroidScript
I know its an old thread but...

So to clarify for a newbie like myself, the "app" in a webview knows nothing of the "app" in the script that created it, so calling app.GetObjects() in a webview would return an empty array?

Dave Smart

unread,
May 28, 2016, 8:31:57 AM5/28/16
to DroidScript
Yep, that is correct.
Message has been deleted

John LaDuke

unread,
May 29, 2016, 5:28:03 AM5/29/16
to DroidScript
...that's OK, I got SaveText and LoadText to pass a string back from the webview. I attached a filepicker as proof of concept.
WebViewFilePicker.spk

Steve Garman

unread,
May 29, 2016, 5:49:22 AM5/29/16
to DroidScript
You can avoid using LoadText and SaveText by calling a function outside your html using
app.Execute()

Have you seen this thread?

https://groups.google.com/d/msg/androidscript/9UStRzvMBaI/W3jiBL0ACgAJ

Reply all
Reply to author
Forward
0 new messages