使用JavaScript解码URL参数 [英] Decoding URL parameters with JavaScript

查看:119
本文介绍了使用JavaScript解码URL参数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这应该是简单的任务,但我似乎无法找到解决方案。

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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆