1

QUESTION:
What do I have to do to make webView.loadUrl(javaScript) work in API 18 or lower (in comparison to API 19 or higher)?

DESCRIPTION OF PROBLEM:
I am using WebView to make a chat.

  1. First I load the HTML like so: webView.loadUrl(url);
  2. Then I use loadUrl again to send in the chat message like so: webView.loadUrl(javaScript);

This works perfectly fine in API >=19 but does not work in API <=18.

Chat messages do still get received from other devices but the other devices do not get a single message from the device with API 18 or lower. The exact same code is used.

I've searched a lot and will continue to search. If you have the answer, any idea that I could test or could redirect me to relevant information please do share. Thank you in advance.

EDIT:
The JavaScript tag in the string is set to lower case (with the same problem): webView.loadUrl("javascript:" + javaScriptFunction);

Name
  • 41
  • 1
  • 6

2 Answers2

3

SOLUTION

I found the problem which was that the (encoded) message parameter sent in the JavaScript function had a line break [ \n ] in it. Removing that using the split function msg.split("\n"); made it work.

Name
  • 41
  • 1
  • 6
  • Is that realy what helps you? `WebView wv = new WebView(mContext);`
    `WebSettings settings = wv.getSettings();`
    `settings.setJavaScriptEnabled(true);`

    `JavaBridgeToJS bridgeToJS = new JavaBridgeToJS();`
    `wv.addJavascriptInterface(bridgeToJS, "javaCallback");`
    `wv.loadUrl("javascript: "var result=foo(); window.javaCallback.result(result)");`
    while it's working just fine on API > 18, the callback is not called on 18 :( and there is no line break.
    – ThinkDeep May 31 '16 at 07:29
1

I can't be sure this is what's causing your problem, but try using

webView.loadUrl("javascript:" + theJavaScriptCode);

with lowercase "javascript:".

API 19+ doesn't care about the case of the JavaScript protocol in loadUrl (though you can use evaluateJavascript() instead anyway in API 19+), but API 18 and down doesn't recognize it unless it's written in lower case.

Alex Forcier
  • 111
  • 6
  • I do have the "javascript:" tag in lower case so that's not it. Thank you though for sharing this information. Do you have any other suggestions? This really confuses me especially when I search for similar problems and find that people have problem making it work API >= 19 instead of API <= 18 as in my case. – Name Jun 22 '15 at 08:28