Why is decodeURIComponent ('%') blocking my browser?

I just tested something with AJAX, and I found that with success, if I warn

alert(decodeURI('%')); 

or

 alert(encodeURIComponent('%')); 

browser errors 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 fine.
Is this something I missed?

+10
javascript
source share
7 answers

Chrome barfs when trying from the console. This gives a URIError: URI incorrectly. % is an escape character; it cannot be by itself.

+15
source share

The fact is that if you use a single % , it interrupts the logic of the decodeURIComponent() function, since it expects that a double-digit data value will be displayed immediately after it, for example %20 (space).

There is a hack. First you need to check whether decodeURIComponent() can actually be executed on a given line and if you do not return the line as 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; } 

Duration:

 decodeURIComponent("%Directory%20Name%") 

will result in an Uncaught URIError: URI malformed error Uncaught URIError: URI malformed

but

 decodeURIComponentSafe("%Directory%20Name%") // %Directory%20Name% 

will return the original string.

If you want to have a fixed / correct URI and % turn into %25 , you need to pass 1 as an additional parameter for the custom function:

 decodeURIComponentSafe("%Directory%20Name%", 1) // "%25Directory%20Name%25" 
+6
source share

The problem here is that you are trying to decode % . This is not a valid encoded string. I think you want to encode % instead.

 decodeURI('%') // URIError encodeURI('%') // '%25' 
+4
source share

Recently, decodeURIComponent in my code worked on the % ampersand, and a google search led me to this question.

Here is the function that I use to handle % , which is shorter than the Ilia version:

 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 the input is empty
  • replaces every % NOT followed by a two-digit (hexadecimal) number by %25
  • returns a decoded string

It also works with other samples here:

  • decodeURIComponentSafe("%%20Visitors") // % Visitors
  • decodeURIComponentSafe("%Directory%20Name%") // %Directory Name%
  • decodeURIComponentSafe("%") // %
  • decodeURIComponentSafe("%1") // %1
  • decodeURIComponentSafe("%3F") // ?
+3
source share

Both decodeURI('%') and decodeURIcomponent('%') cannot work because the URL is in the wrong format (one% is not valid as a URL or URL component)

 Uncaught URIError: URI malformed 

encodeURIComponent() works

+2
source share

You can encode a string before decoding: decodeURI(encodeURI(yourString))

 let str = "100% success!" let transform = encodeURI(str) console.log('transform first', transform) let result = decodeURI(transform); console.log('result ', result) console.log('-----------------') console.log('decode directly: ') console.log(decodeURI(str)) 

-one
source share

An infinite loop or lock can be caused by an error in jquery.

You can set a breakpoint in jquery at a point that is likely to cause a "lock".

A decode does not make sense only under the condition % , since percent-encoding followed by alphanumeric characters that belong to the specified character in the ASCII table, and usually should give a URIError in Opera, Chrome, FF.

Use the browser built into the encodeURI function if you are looking for the "encoding with URL" percent symbol:

 encodeURI('%') //>"%25" 
-7
source share

All Articles