Asim Krishna Das Asim Krishna Das - 5 months ago 12
Android Question

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" />

Answer

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

webView.loadUrl("javascript:(function() { " +
    "document.getElementsByTagName('header')[0].style.display=\"none\"; " +
    "})()");