Android Calling JavaScript functions in WebView -
i trying call javascript functions sitting in html page running inside android webview. pretty simple code tries below - android app, call javascript function test message, inturn calls java function in android app displays test message via toast.
the javascript function looks like:
function testecho(message){ window.jsinterface.doechotest(message); }
from webview, have tried calling javascript following ways no luck:
mywebview.loadurl("javascript:testecho(hello world!)"); mwebview.loadurl("javascript:(function () { " + "testecho(hello world!);" + "})()");
i did enable javascript on webview
mywebview.getsettings().setjavascriptenabled(true); // register class containing methods exposed javascript mywebview.addjavascriptinterface(myjsinterface, "jsinterface");
and heres java class
public class jsinterface{ private webview mappview; public jsinterface (webview appview) { this.mappview = appview; } public void doechotest(string echo){ toast toast = toast.maketext(mappview.getcontext(), echo, toast.length_short); toast.show(); } }
i've spent lot of time googling around see may doing wrong. examples have found use approach. see wrong here?
edit: there several other external javascript files being referenced & used in html, issue?
i figured out issue : missing quotes in testecho() parameter. how got call work:
mywebview.loadurl("javascript:testecho('hello world!')");
Comments
Post a Comment