Joe Rakhimov
Joe Rakhimov

Reputation: 5083

Android. Webview. Show part of page using Javascript

I want to show part of page: web form which will be filled by user. That web form is inside this tag:

enter image description here

In Java, I am trying to get form by class its name(because there is only one tag with class name "form"). Then I am trying to change content with "form" content(maybe I am doing this wrong). Then trying to show content(only form) in webview.

final WebView webview = (WebView) view.findViewById(R.id.wvCheckInn);

webview.getSettings().setJavaScriptEnabled(true);
webview.setWebViewClient(new WebViewClient() {
    @Override
    public void onPageFinished(WebView view, String url) {
        String javascript = "javascript: var form = document.getElementsByClassName('form');"
                + "var body = document.getElementsByTagName('body');"
                + "body.innerHTML = form.innerHTML;";
        view.loadUrl(javascript);
    }
});

webview.loadUrl("http://businessinfo.uz/service/inn");

Result: webview is showing whole page, which is not good for me. How to show part of page in Webview using Javascript?

Upvotes: 0

Views: 1051

Answers (1)

TV's Frank
TV's Frank

Reputation: 818

In your javascript, document.getElementsByClassName() and document.getElementsByTagName() both return arrays of DOM elements (notice the 's's before "By".) This is different from getElementById(), which returns an element directly, which makes sense since IDs are unique across a valid HTML document, but tags and classes are not.

Access the first element from each array and the javascript works:

body[0].innerHTML = form[0].innerHTML;

Upvotes: 2

Related Questions