[英]Javascript replacing /, str.replace not working
I'm having a really stupid issue where javascript is replacing every '/' with '%2F' in a url. 我遇到了一个非常愚蠢的问题,其中javascript用url中的每个'/'替换为'%2F'。 Here is what i have now: 这是我现在所拥有的:
var url;
url = $(this).val();
url = str.replace('%2F', '/');
window.location.href = $(this).val();
What have I done wrong here? 我在这里做错了什么?
You need to decode the url to convert the special characters back to what they should be (like changing %2F
back to /
). 您需要解码url,以将特殊字符转换回应有的字符(例如将%2F
更改回/
)。 To do this, you can use decodeURI
: 为此,您可以使用decodeURI
:
url = $(this).val();
url = decodeURI(url);
However, sometimes spaces get replaced by +
instead of %20
. 但是,有时空格会被+
代替%20
。 So, to handle these cases, you must replace all +
with %20
before decoding your url. 因此,要处理这些情况,必须在解码url之前将所有+
替换为%20
。
url = $(this).val();
url = url.replace('+', '%20');
url = decodeURI(url);
And now url
is the decoded version of the url. 现在url
是url
的解码版本。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.