admin管理员组文章数量:1415484
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.
4 Answers
Reset to default 1Try 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," "));
本文标签: javaURL decoding in JavascriptStack Overflow
版权声明:本文标题:java - URL decoding in Javascript - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1745233203a2648916.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论