java - URL decoding in Javascript - Stack Overflow

I want to decode a string that has been encoded using the java.URLEncoder.encode() method.I tried using

I want to decode a string that has been encoded using the java.URLEncoder.encode() method.

I tried using the unescape() function in javascript, but a problem occurs for blank spaces because java.URLEncoder.encode() converts a blank space to '+' but unescape() won't convert '+' to a blank space.

I want to decode a string that has been encoded using the java.URLEncoder.encode() method.

I tried using the unescape() function in javascript, but a problem occurs for blank spaces because java.URLEncoder.encode() converts a blank space to '+' but unescape() won't convert '+' to a blank space.

Share Improve this question edited Sep 24, 2013 at 14:34 hopper 13.4k7 gold badges51 silver badges53 bronze badges asked Nov 15, 2008 at 9:53 user25778user25778 6,0714 gold badges23 silver badges12 bronze badges
Add a ment  | 

4 Answers 4

Reset to default 1

Try decodeURI("") or decodeURIComponent("") !-)

Using JavaScript's escape/unescape function is almost always the wrong thing, it is inpatible with URL-encoding or any other standard encoding on the web. Non-ASCII characters are treated unexpectedly as well as spaces, and older browsers don't necessarily have the same behaviour.

As mentioned by roenving, the method you want is decodeURIComponent(). This is a newer addition which you won't find on IE 5.0, so if you need to support that browser (let's hope not, nowadays!) you'd need to implement the function yourself. And for non-ASCII characters that means you need to implement a UTF-8 encoder. Code is available if you need it.

decodeURI[Component] doesn't handle + as space either (at least on FF3, where I tested).

Simple workaround:

alert(decodeURIComponent('http://foo./bar+gah.php?r=%22a+b%22&d=o%e2%8c%98o'.replace(/\+/g, '%20'))) 

Indeed, unescape chokes on this URL: it knows only UTF-16 chars like %u2318 which are not standard (see Percent-encoding).

Try

var decoded = decodeURIComponent(encoded.replace(/\+/g," "));

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745233203a4617770.html

相关推荐

  • java - URL decoding in Javascript - Stack Overflow

    I want to decode a string that has been encoded using the java.URLEncoder.encode() method.I tried using

    6小时前
    10

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信