7

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.

Stacked
  • 841
  • 2
  • 12
  • 23
  • 1
    possible duplicate of [Javascript decodeURI(Component) malformed uri exception](http://stackoverflow.com/questions/9064536/javascript-decodeuricomponent-malformed-uri-exception) – Christian Jan 14 '14 at 22:35
  • Possible duplicate of [Why does decodeURIComponent('%') lock up my browser?](http://stackoverflow.com/questions/7449588/why-does-decodeuricomponent-lock-up-my-browser) – Michał Perłakowski Dec 29 '15 at 09:26

1 Answers1

7

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"
Slicedpan
  • 4,995
  • 2
  • 18
  • 33
  • As mentioned above - I dont have control of the server and need to process the output in my html page. – Stacked Dec 20 '13 at 09:37
  • 1
    You 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? – Slicedpan Dec 20 '13 at 09:38
  • No, there could be other special characters in the URL. Anything which would let me extract parameter value in this condition? – Stacked Dec 20 '13 at 09:40
  • I 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 Dec 20 '13 at 10:20
  • Here is the code : `` – Stacked Dec 20 '13 at 10:47
  • `TypeError: results is null` on line `var uri_encoded = results[1].replace(/%([^\d].)/, "%25$1");`. – Stacked Dec 20 '13 at 11:09
  • the value of results is null. you need to check for this before the line `var uri_encoded = ...` – Slicedpan Dec 23 '13 at 09:08