简体   繁体   English

Android WebView JavaScript没有得到评估

[英]Android WebView JavaScript not getting evaluated

In my WebView I'm setting a WebViewClient in order to evaluate some JavaScript when the page finishes loading: 在我的WebView中,我设置了一个WebViewClient以便在页面完成加载时评估一些JavaScript

webView.setWebViewClient(new WebViewClient() {
    @Override
    public void onPageFinished(WebView view, String url) {
        super.onPageFinished(view, url);
        view.loadUrl("javascript:registerObjectDetailsCallback(" +
                                    "function(details) {" +
                                        "if (details.length == 1) {" +
                                            "window.location.href = 'callback:' + escape(details[0]);" +
                                        "} else if (details.length > 1) {" +
                                            "alert('Error: callback set by registerObjectDetailsCallback was passed multiple selection');" +
                                        "}" +
                                    "}" +
                                ");");
    }
}

I'm doing the same thing in iOS which produces expected results: 我在iOS中做同样的事情,它会产生预期的结果:

-(void) webViewDidFinishLoad:(UIWebView *)webView {
    [serverWebView stringByEvaluatingJavaScriptFromString:
     [NSString stringWithFormat:@"registerObjectDetailsCallback("
                                    "function(details) {"
                                        "if (details.length == 1) {"
                                            "window.location.href = 'callback:' + escape(details[0]);"
                                        "} else if (details.length > 1) {"
                                            "alert('Error: callback set by registerObjectDetailsCallback was passed multiple selection');"
                                        "}"
                                    "})"]];
}

But no joy in android. 但在Android中没有喜悦。 Any ideas? 有任何想法吗? Am I calling it at the wrong time? 我在错误的时间打电话吗? Should I be using WebChromeClient ? 我应该使用WebChromeClient吗?

Try using the Javascript Bridge: 尝试使用Javascript Bridge:

How to call javascript from Android? 如何从Android调用JavaScript?

It looks like that's what you're needing. 看起来这就是您所需要的。

The issue here was a race condition between the webapp and the app. 这里的问题是Web应用程序与应用程序之间的竞争状况。 The methods were working properly. 该方法工作正常。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM