HTML with javascript not loading in android webview

I am trying to display a page containing HTML with javascript in an Android web browser with the code below. But this does not seem to work. Can someone help me.

public class MainActivity extends ActionBarActivity {

WebView browser;


@Override
public void onCreate(Bundle icicle) {
    super.onCreate(icicle);
    setContentView(R.layout.activity_main);
    browser = (WebView) findViewById(R.id.webView);
    browser.setLayerType(View.LAYER_TYPE_SOFTWARE, null);
    browser.setWebViewClient(new Callback());
    browser.getSettings().setJavaScriptEnabled(true);




    loadTime();
}


void loadTime() {

    String page = "<html>"
+"<head>"
+"<title>chat window</title>"
+"<script type=\"text/javascript\">"

 + "var bccbId = Math.random(); document.write(unescape('%3Cdiv id=' + bccbId + '%3E%3C/div%3E'));"
 +" window._bcvma = window._bcvma || [];"
 +" _bcvma.push([\"setAccountID\", \"423771628801258096\"]);"
 +" _bcvma.push([\"setParameter\", \"WindowParameters\", \"vr=&vi=&ve=" + gblQnbVars["gUserEmail"] + "&vp=" + gblQnbVars["gMobileNum"] + "&vn= "+ gblQnbVars["gCustomerFirstName"]+ "&lc=\"]);"
  +"var bcLoad = function(){"
  + " if(window.bcLoaded) return; window.bcLoaded = true;"
   +" var vms = document.createElement(\"script\");" 
   +"vms.type = \"text/javascript\";"
   +" vms.async = true;"
   +" vms.src = ('https:'==document.location.protocol?'https://':'http://') + \"vmss.boldchat.com/aid/423771628801258096/bc.vms4/vms.js\";"
   +"var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(vms, s);"
  +"};"
  +"if(window.pageViewer && pageViewer.load) pageViewer.load();"
 +" else if(document.readyState==\"complete\") bcLoad();"
 +" else if(window.addEventListener) window.addEventListener('load', bcLoad, false);"
 +" else window.attachEvent('onload', bcLoad);"
   +             "function FireBoldChat() {"
                          +"      try {"
                                       +        " _bcvmw.chatWindow({"
                                                                             +  "type: \"chat\","
                                                                             +  "rdid: \"\","
                                                                             +  "cwdid:\"1504531236710990857\","  
                                                                             +  "ve:\"<%=visitor email%>\","
                                                                             +  "vp:\"<%=visitor phone%>\","
                                                                             +  "vn:\"<%=visitor name%>\","                                                                                    
                                                                             +  "embed: true"
                                             +  "});"
                               +" } catch (e) {"
                                                +"setTimeout(FireBoldChat, 500)"
                               +" }"
               +" };"
   +" </script>"


+"</head>"
+"<body onload=\"FireBoldChat();\">"
+"</body>"
+"</html>";

    System.out.println(page);
    browser.loadDataWithBaseURL("x-data://base", page,
            "text/html", "UTF-8",
            null);

}

private class Callback extends WebViewClient {
    public boolean shouldOverrideUrlLoading(WebView view, String url) {
        loadTime();

        return(true);
    }
}

Whenever I load this webpage into the default browser, it works fine. Where am I wrong?

+4
source share
5 answers

The documentation for loadData()indicated

, JavaScript , script, , , , , "", "http (s)". , loadDataWithBaseURL() URL.

loadDataWithBaseURL(), URL x-data://base, script http (s)://vmss.boldchat.com. , .

+1

.

vms.src="https://" +\"vmss.boldchat.com/aid/423771628801258096/bc.vms4/vms.js\";"
+1

, java script

, , XML

<?xml version="1.0" encoding="utf-8"?>
<WebView  xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/webview"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
/>

: http://developer.android.com/guide/webapps/webview.html

MainActivity {

@Override
protected void onCreate(Bundle icicle) {
super.onCreate(icicle);
setContentView(R.layout.activity_main);
WebView webView = (WebView)
browser =  findViewById(R.id.webview);
browser.setLayerType(View.LAYER_TYPE_SOFTWARE, null);
browser.setWebViewClient(new Callback());
browser.getSettings().setJavaScriptEnabled(true);


}

}

,

+1
source

Looking at your code, it did not see runtime changes in the html file. Therefore, create an html file using the page variable above and paste it into the data folder.
after that write the following code:

    public class ViewWeb extends Activity {  
        @Override  
        public void onCreate(Bundle savedInstanceState) {  
            super.onCreate(savedInstanceState);
            setContentView(R.layout.webview);  
            WebView vistaWeb = (WebView) findViewById(R.id.webView1); 
            vistaWeb.setWebChromeClient(new Callback());
            vistaWeb.setWebViewClient(new Callback());
            vistaWeb.clearCache(true);
            vistaWeb.clearHistory();
            vistaWeb.getSettings().setJavaScriptEnabled(true);
      vistaWeb.getSettings().setJavaScriptCanOpenWindowsAutomatically(true); 
           vistaWeb.loadUrl("file:///android_asset/aboutcertified.html");   // now it will not fail here
        }  
    }

Hope this helps you.

+1
source

I suspect a variable. gblQnbVarsI did not find a link to this fragment in it. Can you confirm whether it is available in your code? Perhaps this is a throwing mistake.

+1
source

All Articles