Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

The HTML with javascript is not getting loaded in android webview

I am trying to display a page containing HTML with javascript in android webview with the below code.But this doesn't seem to work.Can anyone help me out.

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 my this webpage in default browser its working perfectly. Where i have did wrong.

like image 341
MobilityApp999 Avatar asked Feb 23 '26 16:02

MobilityApp999


1 Answers

The documentation for loadData() says

Note that JavaScript's same origin policy means that script running in a page loaded using this method will be unable to access content loaded using any scheme other than 'data', including 'http(s)'. To avoid this restriction, use loadDataWithBaseURL() with an appropriate base URL.

Now you do use loadDataWithBaseURL(), but your base URL is x-data://base yet you try to load a script from http(s)://vmss.boldchat.com. I think that can cause your problem.

like image 165
ytg Avatar answered Feb 25 '26 04:02

ytg



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!