Search code examples
javascriptescapingencode

passing special characters with encodeURI in javascript


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');
    }