How to decode parameter with space in URL using javascript?
I try to extract parameter from URL, but one parameter has space which is replaced with "+", so the parameter I extract is "iphone+4", but actually it is "iphone 4", how can I convert to the second form, decodeURIComponent does not work he开发者_Go百科re.
function decodeParameter(param) {
return decodeURIComponent(param.replace(/\+/g, ' '));
}
"iPhone+4".replace("+"," ");
That should do it?
It is an ambiguous thing, because you don't really know whether the +
means a space or an actual plus sign. If you are also responsible for creating the URLs, you can solve this by using an appropriate URL encoding function which will use %20 to encode spaces. If you are just collecting them from somewhere else, well, you are left with the option of assuming that every +
means a space :).
You can replace all +
s using this code:
your_text.replace(/\+/g," ");
精彩评论