Search code examples
javascripturiurldecode

Javascript URI decode not performing as expected


I am using the following javascript method to decode a URL.

decodeURIComponent();

When entering the encoded version in the browser it shows the expected image. Following the use of the above decode function, the new URL does not display the image.

How can I decode this properly?

The URL is:

Encoded:

"https://external.xx.fbcdn.net/safe_image.php?d=AQB9-zMODCId1OZa&w=128&h=128&url=https%3A%2F%2Fscontent.xx.fbcdn.net%2Fhprofile-xpa1%2Fv%2Ft1.0-1%2Fp200x200%2F12289537_1025015047521253_6948708859100232371_n.png%3Foh%3Dd4b09489497f8705ca75467bd6af54e9%26oe%3D578DF677&upscale=1&ext=jpg

Decoded:

"https://external.xx.fbcdn.net/safe_image.php?d=AQB9-zMODCId1OZa&w=128&h=128&url=https://scontent.xx.fbcdn.net/hprofile-xpa1/v/t1.0-1/p200x200/12289537_1025015047521253_6948708859100232371_n.png?oh=d4b09489497f8705ca75467bd6af54e9&oe=578DF677&upscale=1&ext=jpg"


Solution

  • The decodeURIComponent() function isn't decoding your URI the way you expect because it's really only intended to work with strings that were previously encoded with encodeURIComponent().

    Instead, you want to use decodeURI().

    var url = 'https://external.xx.fbcdn.net/safe_image.php?d=AQB9-zMODCId1OZa&w=128&h=128&url=https%3A%2F%2Fscontent.xx.fbcdn.net%2Fhprofile-xpa1%2Fv%2Ft1.0-1%2Fp200x200%2F12289537_1025015047521253_6948708859100232371_n.png%3Foh%3Dd4b09489497f8705ca75467bd6af54e9%26oe%3D578DF677&upscale=1&ext=jpg';
    var decoded = decodeURI(url);
    document.write(decoded);