HTML TextArea Characters Limit Inside Android WebView
Asked Answered
E

1

2

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?

Elman answered 15/2, 2011 at 10:22 Comment(2)
I hope this syntax {document.getElementsByTagName('textarea')} in place of {document.getElementsByTagName('textarea')[0]} is not any error here. Please clarify.Poach
yes this is working fine. TextArea gets filled with simple text perfectly but when it comes to fill it with HTML it breaksElman
P
0

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.

Poach answered 16/2, 2011 at 7:34 Comment(3)
Good call! I experimented after your answer and found out these are special characters which are breaking it. See my this post goo.gl/jRgMS The only character I found which was breaking it was "\n" and I need to replace it with "\\n" am I right? Are there other characters which I would need to replace too?Elman
No ! You need to replace it with <br/>Poach
Escape Sequence Character ListPoach

© 2022 - 2024 — McMap. All rights reserved.