【问题标题】:How to call variable from java inside the js in webview如何在webview的js中从java调用变量
【发布时间】:2020-05-25 06:40:42
【问题描述】:

我在 webview 应用程序中自动填写用户名框。但是我需要用java代码中定义的wariable来填补空白,这是我的代码。我想在“PASS”和“NAME”的 java 代码中使用变量

javascript:

document.getElementById('horde_user').value='NAME';" +"javascript:document.getElementById('horde_pass').value='PASS';"

java:

final String js =  "javascript:document.getElementById('horde_user').value='NAME';" +"javascript:document.getElementById('horde_pass').value='PASS';"+"javascript:documentgetElementsByName('login_button')[0].click()";
webview.setWebViewClient(new WebViewClient() {
    public void onPageFinished(WebView view, String url){
        if(Build.VERSION.SDK_INT >= 19){
            view.evaluateJavascript(js, new ValueCallback<String>() {
                @Override
                public void onReceiveValue(String s) {
                }
            });
        }
    }
});

【问题讨论】:

    标签: javascript java android-studio variables webview


    【解决方案1】:

    我建议您创建一个 javascript 函数,例如:

    function login(NAME, PASS)
    {
        document.getElementById('horde_user').value=pass;
        document.getElementById('horde_pass').value=id;
        document.getElementsByName('login_button')[0].click();
    }
    

    然后从你的应用代码

    String id = "myid"; //Get val from the dialog
    String pass = "mypass"; //Get val from the dialog
    webview.loadUrl("javascript:login('"+NAME+"','"+PASS+"')");
    

    如果你需要从 js 调用 java,你可以关注这个post 希望对您有所帮助;)

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2013-05-28
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-08-12
      • 1970-01-01
      • 2020-06-19
      相关资源
      最近更新 更多