5

I have a javascript function 'gotoMainPage()'

function gotoMainPage( ) {
    window.location.href = "main/main.do";
}


Now, WebViewClient's shouldOverrideUrlLoading(..) gets called if gotoMainPage( ) is executed as a result of a 'direct user interaction', such as user clicking on this div:
<div.... onclick='gotoMainPage();'/>

However, if the execution is done via setTimeout( gotoMainPage, 100 ); or via an XMLHttpRequest callback, shouldOverrideUrlLoading(..) is never called but the requested page is loaded into the webview.

Am I missing an obvious explanation or is this a bug?

Anyone?

2 Answers2

6

In my case, when using window.location = "http://xxx" in my webpage, the event shouldOverrideUrlLoading() is not triggered.

However, if I use a custom url scheme or protocol such as "androidurl://", shouldOverrideUrlLoading() is fired. My workaround would to be use a custom protocol and add the following code in the shouldOverrideUrlLoading() method:

if (url.startsWith("androidurl://")) {
    url = url.replaceAll("androidurl://", "http://");
}

This will change the custom protocol back to the "http://" protocol and you can handle the correct url from there.

This works for me.

Tze Min
  • 71
  • 1
  • 2
3

I've seen this come up myself as well, which imho, its clearly a bug. Perhaps you can latch on to :

@Override
public void onLoadResource (WebView view, String url)
{

}

and/or

@Override
public void onPageFinished(WebView webView, String url) 
{

}

I tried this myself, and found that onLoadResource would be triggered, even if shouldOverride wasnt.

Skela
  • 884
  • 10
  • 18