0

I'm looking to encode special characters(ex: Japanese chars) along with '. encodeURIComponent() encodes special characters, but does not encode '.

Any inbuilt Javascript function which does both(i.e encode Japanese chars as well as '?

4
  • 3
    What kind of encoding are you looking for? Commented Nov 25, 2010 at 7:30
  • encodeURIComponent doesn't need to encode ', because that's a valid character in a URI. Why would you want to encode '? Commented Nov 25, 2010 at 9:51
  • I want to encode all the special characters with a function which is char-set safe. ' collides with some strings which already contains '. Commented Nov 25, 2010 at 10:23
  • I fear there's no built in function for that, so use encodeURIComponent and replace its return value yourself using regular expression. Commented Nov 25, 2010 at 11:55

1 Answer 1

1

Try escape and unescape.


Update: OK, escape/unescape aren't I18N friendly. You say encodeURIComponent gets you most of the way there, but misses a few chars, namely '. We can make a helper function utf8escape that uses encodeURIComponent but also takes care of any remaining chars to escape, namely ':

 <html><head><title></title>
        <script>
            function utf8escape(s) {
                s = encodeURIComponent(s);
                s = s.replace(/'/, '%27');
                return s;
            }
            function enc() {
                var f1 = document.getElementById("f1");
                f1.value = utf8escape(f1.value);
            }
            function dec() {
                var f1 = document.getElementById("f1");
                f1.value = decodeURIComponent(f1.value);
            }
        </script>
    </head>
    <body>
        <input type="text" id="f1" name="f1" size="80"/><br/>
        <button onclick="enc()">Encode</button>
        <button onclick="dec()">Decode</button>
    </body>
</html>

This implementation is maybe inefficient, but you get the general idea, I guess.

Sign up to request clarification or add additional context in comments.

6 Comments

Unfortunately these 2 functions are not char-set safe. Not good for I18N.
I didn't quite get you @Mike.
@pavanlimo: sorry, I had a typo. I updated my answer. Hopefully it is relevant and helpful.
For what it's worth, I don't think this is an entirely uncommon problem. I was working on a project using java.net.URLEncoder, and we ran into a problem with its encoding spaces as +. We needed spaces encoded as %20. We looked at uriEscaper from the Google GData library, but in this particular instance, we chose to post-process the output of URLEncoder rather than introduce a third party library. It wasn't an I18N issue, of course, but similar anyhow.
Why can't we have an inbuilt function which encodes each and every character?!!!. Isn't it a simple and a usecase?
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.