Why does decodeURIComponent('%') lock up my browser?
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).
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.
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
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:
decodeURIComponentSafe("%Directory%20Name%", 1) // "%25Directory%20Name%25"
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.
Recently a decodeURIComponent
in my code tripped over the ampersand %
and googling led me to this question.
Here's the function I use to handle %
which is shorter than the version of 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
It also works with the other samples around here:
decodeURIComponentSafe("%%20Visitors") // % Visitors
decodeURIComponentSafe("%Directory%20Name%") // %Directory Name%
decodeURIComponentSafe("%") // %
decodeURIComponentSafe("%1") // %1
decodeURIComponentSafe("%3F") // ?