Search code examples
javascriptandroidhtmlwebviewloaddata

WebView does not load javascript codes


I am trying to open an html page on webview using loadData method with the string parameter which has the html + javascript code.

But the result is not what I wanted. As I understand it did not load javascript parts even though I enabled javascript.

If it is not possible with webview, is there another way to show my page on android phone which supports javascript codes? Thanks in advance.

enter image description here

This is my related code:

 WebView mWebView = (WebView) findViewById(R.id.activity_main_webview);
 WebSettings webSettings = mWebView.getSettings();
 webSettings.setJavaScriptEnabled(true);
 mWebView.loadData(htmlData, "text/html; charset=utf-8", "UTF-8");

Solution

  • I have solved the problem finally. The htmlData contains unexpected characters which are \n and \u0000 and <!-- etc. I replaced my string by deleting them and now it works. Thanks for all.

    htmlData = htmlData.replace("\\n", "");
    htmlData = htmlData.replace("\\t", "");
    htmlData = htmlData.replace("\\u0000", "");
    htmlData = htmlData.replace("\\", "");
    htmlData = htmlData.replace("\"\"\"", "");
    htmlData = htmlData.replace("<!--function", "function");
    htmlData = htmlData.replace("<!-- about:blank -->", "");