Android - 使用webview加载URL后,我可以更改背景颜色

时间:2013-09-01 04:15:20

标签: javascript android security webview

我有一个webview,我正在加载外部HTML表单。我尝试使用javascript函数更改背景颜色:

    function changeBGC(color){
document.bgColor = color;
}

这不起作用。但如果我在本地加载然后我能够改变背景颜色。是否有某种安全措施阻止我将网页更改为外部加载到webview中?

1 个答案:

答案 0 :(得分:6)

您可以使用WebViewClient example here运行javascript。

changes the background color of a document

的javascript代码

所以要把它们放在一起:

启动WebView时:

WebView webview = new WebView();
webview.setWebViewClient(new WebClient());
webView.getSettings().setJavaScriptEnabled(true);
webview.loadUrl("stackoverflow.com");

您的网络浏览客户端:

public class WebClient extends WebViewClient {

    int color;

    public WebClient(int color) {
        this.color = color;
    }
    @Override
    public boolean shouldOverrideUrlLoading(WebView view, String url) {
        view.loadUrl(url);
        return true;
    }

    @Override
    public void onPageFinished(WebView view, String url) 
    {
        String command = "javascript:document.body.style.background = " + color + ";";
        view.loadUrl(command);       
    }
}