user852367 user852367 - 3 years ago 253
HTML Question

Decoding URL parameters with JavaScript

This should simple task, but I can't seem to find a solution.

I have a basic string that is being passed through as a query string parameter like this one:

This+is+a+message+with+spaces
. I would like to decode that parameter using Javascript to
This is a message with spaces
, but I cannot seem to get it to decode.

I've tried
decodeURI('This+is+a+message+with+spaces')
but the result still contains the
+
signs.

Any help would be appreciated. Thank you!

Answer Source

The plus sign is not encoded/decoded. To see the decode function working, you need to pass a encoded URI first. Take a look:

encodeURI( "http://www.foo.com/bar?foo=foo bar jar" )

Will generate: http://www.foo.com/bar?foo=foo%20bar%20jar, i.e., the encoded URI.

decodeURI( "http://www.foo.com/bar?foo=foo%20bar%20jar" )

Will generate: http://www.foo.com/bar?foo=foo bar jar, i.e., the decoded URI.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download