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 '?
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.
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.
encodeURIComponentdoesn't need to encode', because that's a valid character in a URI. Why would you want to encode'?'collides with some strings which already contains'.