Javascript - plus sign changing to whitespace while opening popup window

Last day, one of my colleague approached me with a strange problem. She had a button whose client-side click opens a popup window. At the time of opening, she’s passing a sequence number as querystring value, which contains a plus(+) sign.

But, upon receiving the querystring value, the plus(+) sign was automatically converted to a white-space.


As a quick fix, she thought about replacing the whitespace with plus(+) sign, while reading the querystring. But, that’s not a solution at all. Because, if the sequence contains both plus sign and whitespace, we cannot distinguish them and the above method fails.

Another option was to encode the complete url before calling and decode it at the server-side. But, that will not solve the problem either.

However, the solution is pretty straight forward and simple. Encode the querystring value using encodeURIComponent() method. Please note, encode querystring value ONLY.


Yup! That’s the solution.