有没有办法在webview中拦截javascript触发的URL?

bhu*_*ups 5 javascript android android-webview

有没有办法拦截javascript触发的URL在WebView正常的hrefs使用shouldOverrideUrlLoading()

Jas*_*nCG 5

onLoadResource()在调用任何资源时调用,包括JavaScript.目的有点不同shouldOverrideUrlLoading().

webControls.setWebViewClient(new WebViewClient() { 
    //Notify the host application that the WebView will load 
    //the resource specified by the given url.
    @Override  
    public void onLoadResource (WebView view, String url) {
        super.onLoadResource(view, url);
        Log.v(TAG, "onLoadResource("+url+");");
    }

    //Give the host application a chance to take over the 
    //control when a new url is about to be loaded in the 
    //current WebView. 
    @Override  
    public void shouldOverrideUrlLoading(WebView view, String url) {
        super.shouldOverrideUrlLoading(view, url);
        Log.v(TAG, "shouldOverrideUrlLoading("+url+");");
    }
}
Run Code Online (Sandbox Code Playgroud)