0

I use meta-refresh tag in my html page. The html page has a form. When this page is auto-refreshed, all form parameters are appended to url as HTTP GET parameter.

Problem: All space characters i.e '%20' is converted to '+' automatically. But my FORM Query params may themselves have '+' characters in their content. So

  • How do I stop spaces replaced by '+' and go back to encoding space with '%20'?

OR

  • How do I correctly encode and decode Form params to properly differentiate between space-representing '+' and literal parameter '+' ?

E.G.

properTitle = "This is example for correctly decoding HTTP Form parameters space replaced with '+'" encodeURIComponent(properTitle) : This%20is%20example%20for%20correctly%20decoding%20HTTP%20Form%20parameters%20space%20replaced%20with%20'%2B'

escape(properTitle) : This%20is%20example%20for%20correctly%20decoding%20HTTP%20Form%20parameters%20space%20replaced%20with%20%27+%27

After meta-refresh skewedTitle = "This+is+example+for+correctly+decoding+HTTP+Form+parameters+space+replaced+with '+'"

decodeURIComponent(skewedTitle) : This+is+example+for+correctly+decoding+HTTP+Form+parameters+space+replaced+with+'+

escape(skewedTitle) : This+is+example+for+correctly+decoding+HTTP+Form+parameters+space+replaced+with+%27%252B%27

P.S: I have already referenced following similar questions but no help.

When to encode space to plus (+) or %20?

In a URL, should spaces be encoded using %20 or +?

EDIT 1: Thanks to @Bergi, I have solved my problem by manually encoding '+' character to '%2b' before refresh itself. After meta-refresh '%20' is converted to '+' which is different from '%2b'. Shouldn't there be a proper method to take care of such encoding and decoding ?

Community
  • 1
  • 1
Sourabh
  • 71
  • 2
  • 8
  • 1
    It sounds like your real problem is that the original URL contains `+` characters that should be encoded to `%2B`. You need to address that. – Quentin Feb 19 '15 at 09:56
  • My real problem is space being encoded as '+' which then confuses me if original param too contains '+' in its native form. How do i stop url space being encoded as '+' ? – Sourabh Feb 19 '15 at 10:30
  • 1
    `+` is a perfectly acceptable way of encoding a space in a URL. If the original param contains literal `+` that means `+` instead of a space, then the original param was encoded incorrectly. – Quentin Feb 19 '15 at 10:37
  • How should below param value look when encoded correctly ? "This contains + " – Sourabh Feb 19 '15 at 10:39
  • @Sourabh: Either `This%20contains%20%2B%20` or `This+contains+%2B+` – Bergi Feb 19 '15 at 11:03
  • I'm not sure what you mean by "*all form parameters are appended to url as HTTP GET parameter*". Is the form auto-submitted or what? Are you doing this by JavaScript? If yes, please show the code of that. – Bergi Feb 19 '15 at 11:06
  • Hi @ Bergi, It seems in html, a form params are appended as GET params when refreshed. Pls refer http://stackoverflow.com/questions/2678551/when-to-encode-space-to-plus-or-20#comment2698216_2678602 – Sourabh Feb 19 '15 at 11:10

2 Answers2

0

Use decodeURIComponent() to decode the URL.

EDIT

var str = "Happy%20Day";

Then Do

str = str.replace("%20", " ");
void
  • 36,090
  • 8
  • 62
  • 107
0

Have a look on the following, might help you-

HttpUtility.UrlEncode("Stack Overflow") --> "Stack+Overflow"

Uri.EscapeUriString("Stack Overflow") --> "Stack%20Overflow"

Uri.EscapeDataString("Stack + Overflow") --> Also encodes "+" to "%2b" ---->Stack%20%2B%20%20Overflow

Rajat_RJT
  • 64
  • 7