Call javascript function in Android WebView

后端 未结 3 464
一整个雨季
一整个雨季 2020-12-31 08:58

I have an activity with WebView and Button on it. Android sdk 17. Website isn\'t mine, so I can\'t change it anyway. I need to do js code by android button click.

I\

相关标签:
3条回答
  • 2020-12-31 09:08

    I assume that you are trying to call the test function when you click on the external website you are accessing.

    The way to call the functions you have defined on the Java-side is by the name you assigned when you created the javascript interface, in your case "Android"

    So the call will be "window.Android.test()"

    Also you have to wait to load the website and then inject the javascript code.

    Check out the answer I did some weeks ago with code Hide WebView until JavaScript is done. It's a similar approach to manage the JS and Java using a webview.

    Consider start doing a simple test when the page loads, and later add it inside the javascript click function.

    0 讨论(0)
  • 2020-12-31 09:11

    Change your line with this..

    myWebView.loadUrl("javascript:testEcho('Hello World!')");
    

    try this one might be helpful to you...

    0 讨论(0)
  • 2020-12-31 09:21
    webview.loadUrl("javascript:functionName(\"" + argument + "\")");
    
    0 讨论(0)
提交回复
热议问题