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

时间:2010-08-26 06:50:48

标签: javascript android android-webview

有没有办法拦截WebView中javascript触发的网址,就像使用shouldOverrideUrlLoading()的正常href一样?

1 个答案:

答案 0 :(得分: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+");");
    }
}