Count bytes in textarea using javascript

后端 未结 10 627
难免孤独
难免孤独 2020-12-02 11:15

I need to count how long in bytes a textarea is when UTF8 encoded using javascript. Any idea how I would do this?

thanks!

10条回答
  •  生来不讨喜
    2020-12-02 11:21

    How about simple:

    unescape(encodeURIComponent(utf8text)).length
    

    The trick is that encodeURIComponent seems to work on characters while unescape works on bytes.

提交回复
热议问题