Calling JS function from Android Activity

Issue

I am calling a simple JS function to set values of some html contents, but its not working Here is the JS function

function SetEdits(name,email,pic,date)
{
document.getElementById("myPic").srcpic;
document.getElementById("fullname").valuename;
document.getElementById("email").valueemail;
}

and here is the code from android activity

edit.loadUrl("edit.html");
edit.loadUrl("javascript:SetEdits('"+name+"','"+email+"','"+picture+"','"+date+"')");

its not settings these fileds.. is there any problem with the synax where i am calling this function in native activity?

Solution

You’re probably ending up evaluating the JavaScript before the “edit.html” page has loaded. Try this:

// I'm assuming the real path is something like file:///android_asset/edit.html
edit.loadUrl("edit.html");

edit.setWebViewClient(new WebViewClient() {
  @Override
  public void onPageFinished(WebView view, String url) {
    if (Uri.parse(url).getPath()  "edit.html") {
      view.loadUrl("javascript:SetEdits('" + name+"','" + email + "','" +
                   picture + "','" + date + "')");
    }
  }

Answered By – marcin.kosiba

Leave a Comment