Android WebView ignoring target=“_blank” when adde

2019-02-09 16:37发布

问题:

I am facing a strange issue.

In my application, I need to load a static html file based on clicked button in a WebView from assets folder.

Now among 5 html files, one html file contains 15 static links. These links need to redirect user to the mentioned url in a mobile browser. I have used target="_blank" for that purpose as follows in my html file.

<div class="lid"><a href="https://www.irctc.co.in/" target="_blank">Railway Reservation </a></div>

Now, this works fine in a sample application with a simple WebView without any WebViewClient added to it.

But I need a WebViewClient for my other functionalities. So at that time target="_blank" is totally ignored. And the url is opened in a WebView itself.

I found a workaround, that I can use shouldOverrideUrlLoading as follows:

myWebView.setWebViewClient(new WebViewClient(){
    public boolean shouldOverrideUrlLoading(WebView view, String url) {
    // TODO Auto-generated method stub

        if(url.equalsIgnoreCase("https://www.irctc.co.in/"))
        {
            view.getContext().startActivity(
                            new Intent(Intent.ACTION_VIEW, Uri.parse(url)));
            return true;
        }
        else
            return super.shouldOverrideUrlLoading(view, url);
    }
});

So this is opening that particular url in a default browser.

So basically my questions are:

Why is target="_blank" ignored while we use WebViewClient? And is there any other workaround for this issue? Because I have 15 links, which I would need to compare. I can't load all urls in a new browser, as there are a few links, which need to be opened in a same WebView, too.

回答1:

I have an idea for a workaround. Replace all links which should been opened in a new window with a custom schema. Then you can handle that by your own.

For a minimal interruption set also a custom theme and handle all configuration changes:

<activity android:name="com.example.LinkHandler"
          android:configChanges="keyboard|keyboardHidden|orientation|screenLayout|uiMode|screenSize|smallestScreenSize"
          android:theme="@android:style/Theme.Translucent">
    <intent-filter>
        <action android:name="android.intent.action.VIEW"/>

        <category android:name="android.intent.category.DEFAULT"/>
        <category android:name="android.intent.category.BROWSABLE"/>

        <data android:scheme="your.link.handler.schema"/>
    </intent-filter>
</activity>

Then in your link handler you can read the url by using getIntent().getData().

Please also keep in mind that you should handle http and https, I skipped that in my short example above.

Here is an JavaScript example how to rewrite the urls:

for(var i=0; i<document.links.length; i++) {
    if(document.links[i].target == "_blank") {
        document.links[i].href = "your.schema.for."+document.links[i].href;
    }
}
<ul>
    <li><a href="http://example.com">Test 1</a></li>
    <li><a href="http://example.net" target="_blank">Test 2</a></li>
    <li><a href="https://example.com" target="_blank">Test 3</a></li>
    <li><a href="https://example.net">Test 4</a></li>
</ul>