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

2 Answers 2

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.

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

1 Comment

Is that realy what helps you? WebView wv = new WebView(mContext); <br/> WebSettings settings = wv.getSettings();<br/> settings.setJavaScriptEnabled(true); <br/> <br/> JavaBridgeToJS bridgeToJS = new JavaBridgeToJS();<br/> wv.addJavascriptInterface(bridgeToJS, "javaCallback");<br/> wv.loadUrl("javascript: "var result=foo(); window.javaCallback.result(result)");<br/> while it's working just fine on API > 18, the callback is not called on 18 :( and there is no line break.
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.

1 Comment

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.

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.