webview don't display javascript windows.open()

前端 未结 2 1563
梦如初夏
梦如初夏 2021-01-13 18:59

I have a WebView in which i display web content which i have no control over. The content displays fine, but have links which spawn a popup window. The javascri

相关标签:
2条回答
  • 2021-01-13 19:29

    Please check with adding this -

        getSettings().setSupportMultipleWindows(true);
    
    0 讨论(0)
  • 2021-01-13 19:34

    First of all, you need to set the following settings on your WebView:

    WebSettings webSettings = webView.getSettings();
    webSettings.setJavaScriptEnabled(true);
    webSettings.setJavaScriptCanOpenWindowsAutomatically(true);
    webSettings.setSupportMultipleWindows(true);
    

    Then you need to attach a WebChromeClient that overrides onCreateWindow. Your implementation of this method can create a new web view, and display it inside a dialog:

    webView.setWebChromeClient(new WebChromeClient() {
    
            @Override
            public boolean onCreateWindow(WebView view, boolean isDialog, boolean isUserGesture, Message resultMsg) {
                WebView newWebView = new WebView(MyActivity.this);
                WebSettings webSettings = newWebView.getSettings();
                webSettings.setJavaScriptEnabled(true);
    
                // Other configuration comes here, such as setting the WebViewClient
    
                final Dialog dialog = new Dialog(MyActivity.this);
                dialog.setContentView(newWebView);
                dialog.show();
    
                newWebView.setWebChromeClient(new WebChromeClient() {
                    @Override
                    public void onCloseWindow(WebView window) {
                        dialog.dismiss();
                    }
                });
    
                ((WebView.WebViewTransport)resultMsg.obj).setWebView(newWebView);
                resultMsg.sendToTarget();
                return true;
            }
    
    });
    

    Don't forget to set the new web view to the resultMsg, send it to its target and return true, as mentioned in the API documentation.

    0 讨论(0)
提交回复
热议问题