Search code examples
javascriptandroidwebviewwebviewclient

android: webview not loading javascript with custom WebViewClient


I've got a very basic WebView which works until I try to add a custom webViewClient where it stops processing JavaScript. Am I doing something wrong? Is there another way to get rid of the address bar and menu options in the WebView?

    browser = (WebView) findViewById(R.id.webkit);

    WebSettings webSettings = browser.getSettings();
    webSettings.setJavaScriptEnabled(true);

    // uncommenting this line will remove address bar, but also stop JavaScript from loading
    //browser.setWebViewClient(new InternalWebViewClient());

    // even uncommenting this line will stop JavaScript from loading
    //browser.setWebViewClient(new WebViewClient());

    browser.setWebChromeClient(new InternalWebChromeClient());
    if (savedInstanceState != null) {
        browser.restoreState(savedInstanceState);
    } else {
        browser.loadUrl("http://site.with.javascript");
    }

Solution

  • For some reason the webkit runs JS differently than the browser - I ended up getting around the issue by forcing some JS to run with the following line after the page had loaded:

    browser.loadUrl("javascript:document.getElementById('something').do.something()");