The below code error's out with URIError: malformed URI sequence?
when there is a %
sign like 60% - 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.
How to fix the error. In order to fix "Uncaught URIError: URI malformed" errors in your code, you need to ensure that you are passing valid characters to both encodeURI and decodeURI too. If you would like to convert Unicode code points to UTF8, you can use this online tool.
The JavaScript exception "malformed URI sequence" occurs when URI encoding or decoding wasn't successful.
The URIError object represents an error when a global URI handling function was used in a wrong way. URIError is a serializable object, so it can be cloned with structuredClone() or copied between Workers using postMessage() .
The decodeURIComponent() function decodes a Uniform Resource Identifier (URI) component previously created by encodeURIComponent or by a similar routine.
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"
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With