使用JavaScript解码URL参数 [英] Decoding URL parameters with JavaScript
问题描述
这应该是简单的任务,但我似乎无法找到解决方案。
This should simple task, but I can't seem to find a solution.
我有一个基本字符串作为查询字符串参数传递这一个:这个+是+ a + message + with + spaces
。我想用Javascript解码这个参数<
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.
我试过 decodeURI('This + is + a + message + with + spaces')
,但结果仍然包含 +
标志。
I've tried decodeURI('This+is+a+message+with+spaces')
but the result still contains the +
signs.
任何帮助将不胜感激。谢谢!
Any help would be appreciated. Thank you!
推荐答案
加号不编码/解码。要查看解码函数的工作情况,您需要先传递一个编码的URI。看一看:
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" )
会生成: http://www.foo.com/bar?
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" )
将生成: http://www.foo.com/bar?foo=foo bar jar
,即解码的URI。
Will generate: http://www.foo.com/bar?foo=foo bar jar
, i.e., the decoded URI.
这篇关于使用JavaScript解码URL参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!