TAGS :Viewed: 3 - Published at: a few seconds ago

[ Unescaping / decoding a string with a + in it ]

I have some html that is output by PHP's urlencode - basically it's a simple bunch of anchors that look like this:

<a href="#i+have+some+spaces">link1</a>

I'm using jquery to take a look at all these references when they're clicked, however, I can't seem to decode it using any of the browser's internal functions. Do I have to replace +'s by myself?

unescape('Video+Games');    // returns Video+Games
decodeURI('Video+Games');   // returns Video+Games
decodeURIComponent('Video+Games');  // returns Video+Games

Answer 1

I do think you need to do your own .replace(), as in:

href.replace(/\+/g, ' ');

EDIT: I thought I had a MDC document that specified this, but the closest I can find right now is this note.


I may have been thinking of this note from the MDC encodeURIComponent docs:

For application/x-www-form-urlencoded (POST), per http: //www.w3.org/TR/html401/interac...m-content-type, spaces are to be replaced by '+', so one may wish to follow a encodeURIComponent replacement with an additional replacement of "%20" with "+".