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"
%
? It can be encoded (into%25
) usingencodeURI
orencodeURIComponent
, but it cannot be decoded (using either) because it's not a URI. – Ph