Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

decodeURI not fully working

Tags:

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 
like image 406
ixchi Avatar asked Jul 04 '12 06:07

ixchi


People also ask

What is difference between decodeURI and decodeURIComponent?

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.

How do I decode encodeURI?

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).

What does decode URI component do?

The decodeURIComponent() function decodes a Uniform Resource Identifier (URI) component previously created by encodeURIComponent or by a similar routine.

What is Javascript decodeURI?

The decodeURI() function decodes a Uniform Resource Identifier (URI) previously created by encodeURI() or by a similar routine.


2 Answers

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

like image 169
Tobias Krogh Avatar answered Sep 22 '22 22:09

Tobias Krogh


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.

like image 35
Michael Blake Avatar answered Sep 22 '22 22:09

Michael Blake