1

In my existing application javascript escape() function is used to encode a string.

Now, what i want is to support UTF8 characters also. How could i do this.

In brief, I have text field. That, value is encoded and is passed over the network.

For example : escape("Greetings 1") -- prints -- Greetings%201

I followed the below links :

  1. http://www.w3schools.com/jsref/tryit.asp?filename=tryjsref_escape
  2. http://www.w3schools.com/jsref/jsref_escape.asp

Now, i want is to support UTF8 characters for this field also.

Any ideas are appreciated. Please help.

Bergi
  • 630,263
  • 148
  • 957
  • 1,375
vermaraj
  • 634
  • 3
  • 10
  • 34
  • 1
    `escape()` is unicode, but these days you should probably be using `encodeURI()` or `encodeURIComponent()` – adeneo Jul 01 '14 at 09:13
  • I used both of them . But it prints αβγδεζηθ when passing αβγδεζηθ characters. You can also check this link : http://www.the-art-of-web.com/javascript/escape/ – vermaraj Jul 01 '14 at 09:19
  • 1
    Huh, what? `escape("αβγδεζηθ")` gives `"%u03B1%u03B2%u03B3%u03B4%u03B5%u03B6%u03B7%u03B8"`; and `encodeURI("αβγδεζηθ")` gives `"%CE%B1%CE%B2%CE%B3%CE%B4%CE%B5%CE%B6%CE%B7%CE%B8"`. You seem to have some other encoding problems. – Bergi Jul 01 '14 at 09:51
  • @Bergi try in UTF8 encode,in the link shared. Again i am sharing the link : http://www.the-art-of-web.com/javascript/escape/ Just pass the value in text area corresponding to UTF8 encode. This is what i am getting in my web application, instead of ascented characters. – vermaraj Jul 01 '14 at 10:00
  • @vermaraj: Of course [`utf8_encode`](http://www.php.net/manual/en/function.utf8-encode.php) will return rubbish when `αβγδεζηθ` already is UTF8. However, you did ask a JavaScript question and not a PHP question??? – Bergi Jul 01 '14 at 10:17
  • @Bergi I Understand but then why it displays αβγδεζηΠcharactes in web view. Is there any javascript library availlable which support these characters and encode it as well, and display whatever ascented characters i entered in the field. – vermaraj Jul 01 '14 at 10:53
  • 2
    No. You don't need a javascript library. You need to fix the file encoding at your server. – Bergi Jul 01 '14 at 10:57
  • Javascript handles unicode just fine. The problem is with your server code / encoding. – Luaan Jul 01 '14 at 11:55
  • @Bergi. I have been using JSP, Struts. You mean to say the file encoding to be changed in those files. If yes, then i have already made changes in web.xml (deployment descriptor file) and added a filter, and used the following link to that : http://stackoverflow.com/a/138950/3493471, so that whenever the request is processed to the browser, it is passed through filter and character encoding is set to every page. Even i have seen in firebug, in response headers, the character encoding is text/html; charset=UTF8. I don't know why it is still not working. – vermaraj Jul 02 '14 at 02:35

1 Answers1

2

The non-standard escape() function converts code points > 255 into a 16-bit escape (%uxxxx) which is not what you want.

Use encodeURI() or encodeURIComponent() instead.

The information at the W3schools site tend to be questionable at best, and you would be well advised to double check it with other sources.

Nisse Engström
  • 4,738
  • 23
  • 27
  • 42
  • 1
    At least, http://www.w3schools.com/jsref/tryit.asp?filename=tryjsref_escape is horrible because it will html-escape javascript code. – Bergi Jul 01 '14 at 11:30
  • @Bergi: Amazing. I didn't follow that link. I think I'll have to rephrase my sentiment about w3chools. – Nisse Engström Jul 01 '14 at 11:37
  • Bergi: Actually, it's a sort-of-URL-escape of the HTML output. HTML-escape is entirely different (`<` and so on). – Nisse Engström Jul 01 '14 at 11:53
  • OK, it seems to do it only for character codes > 255. The `αβγδεζηθ` which OP used as an example will get escaped to `αβγδεζηθ` - the latter being invalid in a script of course. – Bergi Jul 01 '14 at 11:58