passing special characters with encodeURI in javascript
-
12-09-2019 - |
Question
I have an HTML input field linked to a button with an onclick function in javascript that can pass the textfield value to a textfield of another page. While passing the values from one page to another via an URL request of a JSP, I found out that encoding the values with encodeURI()
gets:
- £ --> £ (2 signs !!)
- ö --> ö (2 signs !!)
When I use Javscript escape()
I get the proper encodings, but unfortunately the + sign would disappear. Is there are better solution to have some sort of stable encoding of characters?
Solution
encodeURI()
encodes using UTF-8 encoding, which is why you see the two signs for encoding one characters (it's normal).
If you want to use escape and still keep the +
sign, you can use the following:
function mod_escape(value) {
value = escape(value);
return value.replace(/\+/g, '%2B');
}
OTHER TIPS
I think you should use encodeURIComponent() on each single key=value pair and decodeURIComponent at second page for each key=value pair.