开发者

HTML TextArea Characters Limit Inside Android WebView

开发者 https://www.devze.com 2023-02-10 07:59 出处:网络
I have written a HTML page which has following code. <div id=\"adsListContainer\" style=\"margin: 5px;\">

I have written a HTML page which has following code.

<div id="adsListContainer" style="margin: 5px;">
    <textarea id="txtDesc" rows="20" cols="50"></textarea>
    <br />
    <input 开发者_运维问答id="txtCounts" value="0" size="8" />
    <input type="button" value="Count" onclick="countChars()" />
</div>

The above html is enclosed in a file named test.html and it's being shown in android's WebView control. Now using Java and WebView's loadURL() function I am executing javascript as to write something in that textarea as following code goes.

javascript:document.getElementsByTagName("textarea")[0].value += 'anything goes here of not more than 50 chars';void(0);

Even thought the above code works and I am calling it many times so I may insert 7000+ characters in this textarea element.

The Java code responsible for executing Javascript to insert text (HTML) into textarea is as follows.

int bStart = 0;
int bEnd = 49;
String description = "some huge description including html tags"
int totalChars = description.length() - 1;

while (bStart <= totalChars) {
    if (bEnd > totalChars)
        bEnd = totalChars;

    rv = "javascript:";

    rv += "var tas=document.getElementsByTagName('textarea');"; // description
    rv += "if (tas.length>0) {";
    rv += "var ta=tas[0];";
    rv += "ta.value += '"
           + description.substring(bStart, bEnd)
            .replace("'", "\\'").replace('"', '\"') + "';"; // description
    rv += "}";

    webView.loadUrl(rv + "void(0);");

    bStart += 50;
    bEnd += 50;
}

Although the above code works but not perfectly. The description has 7245 characters but it only insert 6267 characters into textarea of the web page.

Is there something I am missing?


I have tried to apply your issue only with HTML + JavaScript. Please see this fiddle.

As you can see, this is working properly. So, I think the issue may be with replacement of escape sequence characters. See that you are changing the escape sequence character from within the loop that will increase the size of description variable. But, totalChars has the size which is previously stored size of description. So, later when you apply "bStart <= totalChars" condition, it will count till the totalChars only (i.e. already less then the current size).

Concluding, your description has too many escape sequences characters "may be" generating this problem. So, replace the whole string previously & then add substrings to your javaScript.

This may or may not solve your issue if there is any another one, still. So, please correct me if I am wrong.

Thank you.

0

精彩评论

暂无评论...
验证码 换一张
取 消