简体   繁体   English

Android WebView loadUrl JavaScript无法使用API​​ <= 18(但可以在API> = 19中使用)

[英]Android WebView loadUrl JavaScript not working API <=18 (but works in API >=19)

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)? 为了使webView.loadUrl(javaScript)在API 18或更低版本(与API 19或更高版本相比)下工作,我该怎么办?

DESCRIPTION OF PROBLEM: 问题描述:
I am using WebView to make a chat. 我正在使用WebView进行聊天。

  1. First I load the HTML like so: webView.loadUrl(url); 首先,我像这样加载HTML: webView.loadUrl(url);
  2. Then I use loadUrl again to send in the chat message like so: webView.loadUrl(javaScript); 然后,我再次使用loadUrl发送聊天消息,如下所示: webView.loadUrl(javaScript);

This works perfectly fine in API >=19 but does not work in API <=18. 这在API> = 19中可以很好地工作,但在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. 聊天消息仍然会从其他设备收到,但是其他设备不会从API 18或更低版本的设备收到一条消息。 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); 字符串中的JavaScript标记设置为小写(存在相同的问题): webView.loadUrl(“ javascript:” + javaScriptFunction);

SOLUTION

I found the problem which was that the (encoded) message parameter sent in the JavaScript function had a line break [ \\n ] in it. 我发现了一个问题,那就是JavaScript函数中发送的(编码)消息参数中有一个换行符[ \\ n ]。 Removing that using the split function msg.split("\\n"); 使用拆分功能msg.split(“ \\ n”);删除它 made it work. 使它工作。

I can't be sure this is what's causing your problem, but try using 我不确定这是引起您问题的原因,但请尝试使用

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

with lowercase "javascript:" . 小写的"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. API 19+并不在意loadUrl JavaScript协议的loadUrl (尽管您可以在API 19+中loadUrl也可以使用evaluateJavascript() Javascript evaluateJavascript() ),但是除非使用小写形式,否则API 18及更低版本将无法识别它。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM