Quantcast
Viewing all articles
Browse latest Browse all 19

Answer by Alexander Gladysh for String length in bytes in JavaScript

Actually, I figured out what's wrong. For the code to work the page <head> should have this tag:

<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />

Or, as suggested in comments, if server sends HTTP Content-Encoding header, it should work as well.

Then results from different browsers are consistent.

Here is an example:

<html><head><meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <title>mini string length test</title></head><body><script type="text/javascript">document.write('<div style="font-size:100px">'+ (unescape(encodeURIComponent("ЭЭХ! Naïve?")).length) +'</div>'  );</script></body></html>

Note: I suspect that specifying any (accurate) encoding would fix the encoding problem. It is just a coincidence that I need UTF-8.


Viewing all articles
Browse latest Browse all 19

Trending Articles