Window.location.href javascript does not call shouldOverrideUrlLoading

My nexous tested one (compiled with android 2.2) shows that shouldOverrideUrlLoading does not start when the page is redirected through window.location.href. By default, onPageFinished is a trigger.

Can anyone advise to intercept the redirect of a javascript page? Any other way to redirect the page in javascript, so should shouldOverrideUrlLoading start? Is this an error for shouldOverrideUrlLoading?

Thanks,

June

+4
source share
3 answers

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

However, if I use a custom URL scheme or protocol, for example androidurl:// , shouldOverrideUrlLoading() . 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 will change the native protocol to http:// and you can process the correct URL.

This works for me.

+1
source

Can anyone advise to intercept the redirect of a javascript page?

Modify Javascript to call the method for some object that you enter into the WebView via addJavascriptInterface() .

Is this an error for shouldOverrideUrlLoading?

Maybe. This is certainly a limitation.

0
source

Perhaps you can use WebChromeClient onJsBeforeUnload () to intercept the event you are about to leave on the current page. Override onJsBeforeUnload () and set the flag to true.

Then use WebViewClient onLoadResource () to intercept the download URL. If the flag is true, do whatever you want, for example. open the URL in a different intent; otherwise do nothing.

Also, be sure to add to your HTML:

 <script> window.onbeforeunload = function(){ return "Leave?"; }; </script> 
0
source

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


All Articles