Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

shouldOverrideUrlLoading(...) not executed if "window.location.href" modified in a timeout callback

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?

like image 680
Dr. Benedict Porkins Avatar asked Mar 08 '11 06:03

Dr. Benedict Porkins


1 Answers

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.

like image 57
Tze Min Avatar answered Sep 18 '22 02:09

Tze Min