URIError: malformed URI sequence?
33,551
I think you need to URI encode the percentage sign as '%25'
http://some-external-server.com/info?progress=60%25%20-%20Completed
[EDIT]
I guess you could do something like this:
var str = "60%%20-%20completed";
var uri_encoded = str.replace(/%([^\d].)/, "%25$1");
console.log(str); // "60%25%20-%20completed"
var decoded = decodeURIComponent(uri_encoded);
console.log(decoded); // "60% - completed"
Author by
Stacked
Updated on July 09, 2022Comments
-
Stacked almost 2 years
The below code error's out with
URIError: malformed URI sequence?
when there is a%
sign like60% - Completed
in the URL string from where I need to extract the parameter value e.g.http://some-external-server.com/info?progress=60%%20-%20Completed
<SCRIPT type="text/javascript"> function getParameterByName(name) { name = name.replace(/[\[]/, "\\\[").replace(/[\]]/, "\\\]"); var regex = new RegExp("[\\?&]" + name + "=([^&#]*)"), results = regex.exec(location.search); return results == null ? "" : decodeURIComponent(results[1].replace(/\+/g, " ")); } </SCRIPT>
I dont have control of the server and need to process the output in my html page.
-
Stacked over 10 yearsAs mentioned above - I dont have control of the server and need to process the output in my html page.
-
Slicedpan over 10 yearsYou may not be able to use decodeURIComponent then, or you may need to preprocess the url before passing it to that function. Are spaces the only other special character you can expect in the url?
-
Stacked over 10 yearsNo, there could be other special characters in the URL. Anything which would let me extract parameter value in this condition?
-
Stacked over 10 yearsI tried to use this on the
getParameterByName
function but can't seem to get it working. Can you provide an example in the above code's context for a newbie. -
Stacked over 10 yearsHere is the code :
<SCRIPT type="text/javascript"> function getParameterByName(name) { name = name.replace(/[\[]/, "\\\[").replace(/[\]]/, "\\\]"); var regex = new RegExp("[\\?&]" + name + "=([^&#]*)"), results = regex.exec(location.search); var uri_encoded = results[1].replace(/%([^\d].)/, "%25$1"); return results == null ? "" : decodeURIComponent(uri_encoded.replace(/\+/g, " ")); } </SCRIPT>
-
Stacked over 10 years
TypeError: results is null
on linevar uri_encoded = results[1].replace(/%([^\d].)/, "%25$1");
. -
Slicedpan over 10 yearsthe value of results is null. you need to check for this before the line
var uri_encoded = ...