3

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.

2
  • 1
    You can't make your webpage like this because HTML calls JavaScript. Not the other way around. Commented Jun 1, 2015 at 20:35
  • please give more declaration about "gblQnbVars" base code, because it shows error. Commented Jun 8, 2015 at 8:28

5 Answers 5

1

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.

Sign up to request clarification or add additional context in comments.

Comments

1

I got same problem. I just replace

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

Comments

1

this will be come if not add necessary java script file

before run please check your XML format is ok

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

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

and you may not call layout public class MainActivity extends Activity {

@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);


}

}

try this some time this will be help

thank

Comments

1

Looking at your code, didn't see the run time changes in html file. So create html file using above page variable & put in assets folder.
once this done write 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 will help you.

Comments

1

I suspect the variable gblQnbVars I did not find any reference to it in your snippet. Can you confirm if it is accessible in your code? May be that's throwing error.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.