2

I am using a webview in my activity to show a webpage and I am using javascript to hide the header.

I have tried the following script in chrome console and it works fine: document.getElementsByClassName('Header')[0].style.display = 'none';

When I use the same script in android webview the page gets cleared and it displays none which is the output of the script. (also received on Chrome console).

String s = (new StringBuilder())
  .append(" javascript:  document.getElementsByClassName('Header')[0].style.display = 'none';")
  .toString();
webView.loadUrl(s);

1 Answer 1

1

You can use below code -

    try {

        // Load the html into jsoup
        Document doc = Jsoup.connect("http://your-site.com/").get();

        // find and remove header
        Element header = doc.getElementById("your-header");
        header.remove();

        // find and remove footer
        Element footer = doc.getElementById("your-footer");
        footer.remove();

        // Load data into a WebView
        WebView wv = (WebView) findViewById(R.id.webView);
        WebSettings ws = wv.getSettings();
        ws.setJavaScriptEnabled(true);
        wv.loadData(doc.toString(), "text/html", "utf-8");

    } catch (IOException e) {
        e.printStackTrace();
    }

You will find latest Jsoup Library at this link.

The library can be added to gradle by adding the following dependency compile 'org.jsoup:jsoup:1.8.2'

Sign up to request clarification or add additional context in comments.

3 Comments

@dmsherazi - can you please provide me crash report for this.
Check link
I would use AsynkTask Instead. There is a tutorial for Jsoup at androidbegin.com/tutorial/android-basic-jsoup-tutorial

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.