1

Possible Duplicate:
Count bytes in textarea using javascript

Hello everyone.

I am trying to get the size in bytes of text inside textarea.

by using the following code:

function getSize(obj)
{
    return (document.getElementById(obj).value.length);
}

I get the number of chars. (\n count as one char, no Idea why).

anyway, how can I get the size in bytes of text inside textarea?

Thanks

Community
  • 1
  • 1
Ron
  • 3,975
  • 17
  • 80
  • 130

1 Answers1

2

The size in bytes of a text snippet depends on the encoding which can be arbitrary (overridden by the user agent).

Measuring regular text in bytes makes sense only if the encoding is known. For example, ASCII text is 1 byte per char. Unicode UTF-16LE has 2 bytes. UTF-8 is a whole different story, things can span from 1 byte to 4 bytes.

Saul
  • 17,973
  • 8
  • 64
  • 88
  • Actually, strings in JS are stored in UTF-16 internally. At least, in FF. – kirilloid Feb 07 '11 at 19:42
  • 1
    `Unicode has 2 bytes` - **NO.** [Unicode is not an encoding.](http://stackoverflow.com/questions/3951722/) But, good point about the size depending on the encoding. – Matt Ball Feb 07 '11 at 19:43
  • I want to get the size in bytes of ANSI encoding. – Ron Feb 07 '11 at 19:58
  • @Ron: The size in bytes of ANSI encoding equals to the number of chars. – Saul Feb 07 '11 at 19:59
  • @Saul, no it doest because \n should be 2 bytes but the .length function counts it as one. (\n as new line). – Ron Feb 07 '11 at 20:01
  • Because it's not, it's translated to a newline character, which is only 1 byte –  Feb 07 '11 at 20:09
  • @M28, if u make new like in text file (ANSI encoded) and save the file, the size will be 2 bytes... – Ron Feb 07 '11 at 20:16
  • @Ron: By standard, every ANSI character **is** 1 byte long. Even the newline `\n`. The fact of it having a 2-character visual representation does not mean an actual newline takes up 2 bytes in memory. – Saul Feb 07 '11 at 20:20
  • @Ron In windows, newlines are \r\n (or \n\r, I don't remember). In other systems, it's just \n –  Feb 07 '11 at 20:25
  • @M28: *nix uses `\n` and on Apple it's `\r`. Windows developers wanted to have the best of both worlds but ended up in creating a new headache. – Saul Feb 07 '11 at 20:35