Android WebView: Determine <a> target= "_ blank"

android, javascript, webview

Solution

I just solved this issue myself. Here is how I fixed it.

mWebView.setWebChromeClient(new WebChromeListener() {
    @Override
    public boolean onCreateWindow(WebView view, boolean dialog, boolean userGesture, Message resultMsg) {
        WebView newWebView = new WebView(view.getContext());
        newWebView.setWebViewClient(new WebViewClient() {
            @Override
            public boolean shouldOverrideUrlLoading(WebView view, String url) {
                Intent browserIntent = new Intent(Intent.ACTION_VIEW, Uri.parse(url));
                startActivity(browserIntent);
                return true;
            }
        });
        WebView.WebViewTransport transport = (WebView.WebViewTransport) resultMsg.obj;
        transport.setWebView(newWebView);
        resultMsg.sendToTarget();
        return true;
    }
});

Problem

is it possible to check if the user has clicked on a html link with the target="_blank". What I want to do is to display htlm in my App in a WebView, but start "external" links in the android default browser. A "external" link is for me a link with target="_blank". All other links should be handled in the webview. So for example: the user clicks on a link like this in my WebView: ``` <a href="http://www.google.com" target="_blank">new window</a> ``` and then I want to open the given url in the android browser. I tried it with shouldOverrideUrlLoading(), but at this point I can't determine, if the target was "_blank" or a normal link (without target). I tried also setSupportMultipleWindows(true); in combination with onCreateWindow(), but in this callback I can't get the url. I cant change the HTML that is displayed, so I can't use a JavaScript Bridge with addJavascriptInterface() What else can I do? Any other idea?

Original source