ShouldOverrideUrlLoading (...) is not executed if "window.location.href" is changed in a timeout callback

I have a javascript function 'gotoMainPage ()'

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


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

However, if execution is done through setTimeout( gotoMainPage, 100 ); or through the XMLHttpRequest shouldOverrideUrlLoading(..) , shouldOverrideUrlLoading(..) never called, and the requested page is loaded into the shouldOverrideUrlLoading(..) .

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

Is anyone

+4
source share
3 answers

In my case, when using window.location = "http: // xxx" on my web page, the event shouldOverrideUrlLoading () does not fire.

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

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

This changes the native protocol back to the http: // protocol, and you can process the correct URL from there.

This works for me.

+6
source

I saw that it also occurred to me that imho, this is clearly a mistake. Perhaps you can click:

 @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 will be running even if shouldOverride wasnt.

+3
source

setTimeout is called as follows:

 timeOut = setTimeout("gotoMainPage()", 100); 

You can also

 clearTimeout(timeOut); 
0
source

Source: https://habr.com/ru/post/1342758/


All Articles