Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Removing a Tag from a webpage on android WebView

I want to remove the "header" element from a webpage with the help of this answer Display a part of the webpage on the webview android

But I am getting an error message; see this https://dl.dropboxusercontent.com/u/15065300/Check-2.jpg

Here is my full code:

import android.app.Activity;
import android.os.Bundle;
import android.webkit.WebView;
import android.webkit.WebViewClient;

public class WebViewActivity extends Activity {

    private WebView webView;

    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.webview);

        webView = (WebView) findViewById(R.id.webView1);
        webView.getSettings().setJavaScriptEnabled(true);
        webView.setWebViewClient(new WebViewClient() {
         @Override
        public void onPageFinished(WebView view, String url)
        {
            webView.loadUrl("javascript:(function() { " +
                    "document.getElementsByTagName('myDiv1')[0].style.display="none"; " +
                    "})()");
        }
        });
        webView.loadUrl("http://skyasim.info/abc.html");

    }

}

Manifest permission as well

 <uses-permission android:name="android.permission.INTERNET" />
like image 682
Asim Roy Avatar asked Dec 16 '22 08:12

Asim Roy


2 Answers

You need to escape your ". See the Escape Sequences section.

webView.loadUrl("javascript:(function() { " +
    "document.getElementsByTagName('header')[0].style.display=\"none\"; " +
    "})()");
like image 120
James McCracken Avatar answered Dec 31 '22 14:12

James McCracken


This worked for me:

webView.loadUrl("javascript:var con = document.getElementById('a'); " +
                "con.parentNode.removeChild(con); ");

htmlCode:

    <ul>
............
     <div id="a">               
                <H4 align="center"> something</H4>  
     </div>                 
    </ul>
like image 38
smajdalf Avatar answered Dec 31 '22 13:12

smajdalf