I'm trying to remove the URI encoding from a link, but decodeURI doesn't seem to be fully working.
My example link is this: /linkout?remoteUrl=http%253a%252f%252fsandbox.yoyogames.com%252fgames%252f171985-h-a-m-heroic-armies-marching
After running the JavaScript script, it looks like this:
http%3a%2f%2fsandbox.yoyogames.com%2fgames%2f171985-h-a-m-heroic-armies-marching
How can I get rid of the remaining not correct codes in the URI?
My decoding code:
var href = $(this).attr('href'); // get the href var href = decodeURI(href.substring(19)); // remove the outgoing part and remove the escaping $(this).attr('href', 'http://'+href) // change link on page
decodeURI(): It takes encodeURI(url) string as parameter and returns the decoded string. decodeURIComponent(): It takes encodeURIComponent(url) string as parameter and returns the decoded string.
1. decodeURI function: The decodeURI() function is used to decode URI generated by encodeURI(). Parameters: This function accepts a single parameter complete_encoded_uri_string which holds the encoded string. Return Value: This function returns the decoded string (original string).
The decodeURIComponent() function decodes a Uniform Resource Identifier (URI) component previously created by encodeURIComponent or by a similar routine.
The decodeURI() function decodes a Uniform Resource Identifier (URI) previously created by encodeURI() or by a similar routine.
the url looks as it was encoded twice, I also suggest to use decodeURIComponent
decodeURIComponent(decodeURIComponent("http%253a%252f%252fsandbox.yoyogames.com%252fgames%252f171985-h-a-m-heroic-armies-marching"))
results in: "http://sandbox.yoyogames.com/games/171985-h-a-m-heroic-armies-marching"
but you should check why you have the url encoded twice in advance
I just encountered this situation in an ASHX handler for the PUT verb. ASP.NET is apparently encoding my XML for me, so my server-side call to HttpUtility.UrlEncode was not needed. Fixing it by calling client-side Javascript decodeURI twice -- is closing the barn door after the cows have already left and the HTTP I was sending was a protocol violation.
I would have commented and plus-one'd Tobias Krogh's answer, but I don't have the points to do so…
However, I still think that it is important to note that the failure being discussed here is not a Javascript decodeURI or anything else -- it is a data validation error.
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