[英]window.beforeunload called before WebViewClient.shouldOverrideUrlLoading
In the Android app with the WebView and WebViewClient configured, after the window.open(url, '_system') called from the js, the js event window.beforeunload is fired before the Android WebViewClient.shouldOverrideUrlLoading is called. In the Android app with the WebView and WebViewClient configured, after the window.open(url, '_system') called from the js, the js event window.beforeunload is fired before the Android WebViewClient.shouldOverrideUrlLoading is called. Is this a proper and intended behavior?这是一个适当的和预期的行为吗? Is there a way to intercept the url open without the window.beforeunload event fired?有没有办法拦截 url 打开而不触发 window.beforeunload 事件?
see this example code, maybe help you:看这个示例代码,也许对你有帮助:
final WebView webView = (WebView)findViewById(R.id.webview);
// Enable javascript
webView.getSettings().setJavaScriptEnabled(true);
webView.setWebViewClient(new WebViewClient() {
@SuppressWarnings("deprecation")
@Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
final Uri uri = Uri.parse(url);
return customUriHanlder(uri);
}
@RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
@Override
public boolean shouldOverrideUrlLoading(WebView view, WebResourceRequest request) {
final Uri uri = request.getUrl();
return customUriHanlder(uri);
}
private boolean customUriHanlder(final Uri uri) {
Log.i(TAG, "Uri =" + uri);
final String host = uri.getHost();
final String scheme = uri.getScheme();
// you can set your specific condition
if (true) {
// Returning false means that you are going to load this url in the webView itself
return false;
} else {
// Returning true means that you need to handle what to do with the url
// open web page in a Browser
final Intent intent = new Intent(Intent.ACTION_VIEW, uri);
startActivity(intent);
return true;
}
}
});
// Load the webpage
webView.loadUrl("https://google.com/");
with customUriHanlder
function you can load your url in vebview itself or in browser.使用customUriHanlder
function 您可以在 vebview 本身或浏览器中加载 url。
on google documentation shouldOverrideUrlLoading
: If a WebViewClient is provided, returning true
causes the current WebView to abort loading the URL, while returning false
causes the WebView to continue loading the URL as usual. on google documentation shouldOverrideUrlLoading
: If a WebViewClient is provided, returning true
causes the current WebView to abort loading the URL, while returning false
causes the WebView to continue loading the URL as usual.
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.