Javascript 为什么 decodeURIComponent('%') 会锁定我的浏览器?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/7449588/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Why does decodeURIComponent('%') lock up my browser?
提问by CristiC
I was just testing something with AJAX and I found that on success if I alert
我只是在用 AJAX 测试一些东西,如果我提醒,我发现成功了
alert(decodeURI('%'));
or
或者
alert(encodeURIComponent('%'));
the browser errors out with the following code.
浏览器出错并显示以下代码。
$.ajax({
type: "POST",
url: "some.php",
data: "",
success: function(html){
alert(decodeURIComponent('%'));
// alert(decodeURI('%'));
}
});
If I use any other string it works just fine.
Is it something that I missed?
如果我使用任何其他字符串,它就可以正常工作。
是我错过了什么吗?
回答by Juan Mendes
Chrome barfs when trying from the console. It gives an URIError: URI malformed. The % is an escape character, it can't be on its own.
从控制台尝试时 Chrome barfs。它给出了一个 URIError: URI 格式错误。% 是一个转义字符,它不能单独存在。
回答by Heinrich Ulbricht
Recently a decodeURIComponent
in my code tripped over the ampersand %
and googling led me to this question.
最近decodeURIComponent
,我的代码中的a 被与号绊倒了%
,谷歌搜索使我想到了这个问题。
Here's the function I use to handle %
which is shorter than the version of Ilia:
这是我用来处理%
比 Ilia 版本短的函数:
function decodeURIComponentSafe(s) {
if (!s) {
return s;
}
return decodeURIComponent(s.replace(/%(?![0-9][0-9a-fA-F]+)/g, '%25'));
}
It
它
- returns the input value unchanged if input is empty
- replaces every
%
NOT followed by a two-digit (hex) number with%25
- returns the decoded string
- 如果输入为空,则返回未更改的输入值
- 用
%
两位数(十六进制)数字替换每个NOT 后跟%25
- 返回解码后的字符串
It also works with the other samples around here:
它也适用于这里的其他示例:
decodeURIComponentSafe("%%20Visitors") // % Visitors
decodeURIComponentSafe("%Directory%20Name%") // %Directory Name%
decodeURIComponentSafe("%") // %
decodeURIComponentSafe("%1") // %1
decodeURIComponentSafe("%3F") // ?
decodeURIComponentSafe("%%20Visitors") // % Visitors
decodeURIComponentSafe("%Directory%20Name%") // %Directory Name%
decodeURIComponentSafe("%") // %
decodeURIComponentSafe("%1") // %1
decodeURIComponentSafe("%3F") // ?
回答by Ilia Rostovtsev
The point is that if you use single %
it breaks the logic of decodeURIComponent()
function as it expects two-digit data-value followed right after it, for example %20
(space).
关键是,如果您使用 single%
它会破坏decodeURIComponent()
函数的逻辑,因为它期望紧随其后的两位数据值,例如%20
(空格)。
There is a hack around. We need to check first if the decodeURIComponent()
actually can run on given string and if not return the string as it is.
周围有黑客。我们需要首先检查是否decodeURIComponent()
真的可以在给定的字符串上运行,如果不能按原样返回字符串。
Example:
例子:
function decodeURIComponentSafe(uri, mod) {
var out = new String(),
arr,
i = 0,
l,
x;
typeof mod === "undefined" ? mod = 0 : 0;
arr = uri.split(/(%(?:d0|d1)%.{2})/);
for (l = arr.length; i < l; i++) {
try {
x = decodeURIComponent(arr[i]);
} catch (e) {
x = mod ? arr[i].replace(/%(?!\d+)/g, '%25') : arr[i];
}
out += x;
}
return out;
}
Running:
跑步:
decodeURIComponent("%Directory%20Name%")
will result in Uncaught URIError: URI malformed
error
会导致Uncaught URIError: URI malformed
错误
while:
尽管:
decodeURIComponentSafe("%Directory%20Name%") // %Directory%20Name%
will return the initial string.
将返回初始字符串。
In case you would want to have a fixed/proper URI and have %
turned into %25
you would have to pass 1
as additional parameter to the custom function:
如果你想要一个固定/正确的 URI 并且已经%
变成%25
你必须1
作为附加参数传递给自定义函数:
decodeURIComponentSafe("%Directory%20Name%", 1) // "%25Directory%20Name%25"
回答by Rocket Hazmat
The problem here is you're trying to decodethe %
. This is not a valid encoded string. I think you want to encodethe %
instead.
这里的问题是你想解码的%
。这不是有效的编码字符串。我想你要编码的%
替代。
decodeURI('%') // URIError
encodeURI('%') // '%25'
回答by genesis
Both decodeURI('%')
and decodeURIcomponent('%')
cannot work because the URL is malformed (a single % is not valid as a url or url component)
双方decodeURI('%')
并decodeURIcomponent('%')
不能工作,因为URL是错误的(单一%是不是一个网址或网址成分有效)
Uncaught URIError: URI malformed
encodeURIComponent()
works
encodeURIComponent()
作品
回答by JnK
I had the same issue as OP and found this useful topic. Had to find a way to check if URI string contained percent sign before using decodeURIComponent().
我遇到了与 OP 相同的问题,并发现了这个有用的话题。在使用 decodeURIComponent() 之前,必须找到一种方法来检查 URI 字符串是否包含百分号。
The piece of code from Ilia Rostovtsevworks great except for URI which contains encoded characters like %C3 (where percent sign is starting by [A-F]) because the regex used doesn't handle them (only % followed by a decimal).
来自Ilia Rostovtsev的一段代码工作得很好,除了 URI 包含像 %C3 这样的编码字符(百分号以 [AF] 开头),因为使用的正则表达式不处理它们(只有 % 后跟小数)。
I replaced the following line:
我替换了以下行:
x = mod ? arr[i].replace(/%(?!\d+)/g, '%25') : arr[i];
by
经过
x = mod ? arr[i].replace(/%(?!\d|[ABCDEF]+)/g, '%25') : arr[i];
Now, it is working as the regex will reject %1A to %9F and also %A1 to %F9
现在,它正在工作,因为正则表达式将拒绝 %1A 到 %9F 以及 %A1 到 %F9
回答by Lorenz Lo Sauer
The endless-loop or lock up may be due to a bug in jquery.
无限循环或锁定可能是由于 jquery 中的错误。
You can set a breakpoint in jquery at a point which is likely causing the 'lock-up'.
您可以在 jquery 中可能导致“锁定”的位置设置断点。
Decode doesn't make sense with just %
provided, as percent-encoding
is followed by alphanumericals referring to a given character in the ASCII table, and should normally yield an URIError in Opera, Chrome, FF.
仅%
提供解码没有意义,因为percent-encoding
后面是指代 ASCII 表中给定字符的字母数字,并且通常应该在 Opera、Chrome、FF 中产生 URIError。
Use the browser built in function encodeURI
if you are looking for the 'url-encoded' notation of the percent-character:
encodeURI
如果您正在寻找百分比字符的“url-encoded”符号,请使用浏览器内置函数:
encodeURI('%')
//>"%25"