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.
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:
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: