47

How can I decode a URL using jQuery? My url is

http%3A%2F%2Fdtzhqpwfdzscm.cloudfront.net%2F4ca06373624db.jpg

Andy E
  • 338,112
  • 86
  • 474
  • 445
XMen
  • 29,384
  • 41
  • 99
  • 151
  • There is no way to do this with jQuery, because all browsers implement the decodeURIComponent function for Javascript, as Darin's answer below explains - consider replacing "jquery" tag w/ "javascript"? – jrz Apr 19 '13 at 15:54

5 Answers5

105

Try the decodeURIComponent function:

var decodedUri = decodeURIComponent('http%3A%2F%2Fdtzhqpwfdzscm.cloudfront.net%2F4ca06373624db.jpg');
alert(decodedUri);
Darin Dimitrov
  • 1,023,142
  • 271
  • 3,287
  • 2,928
  • 15
    But that doesn't use jQuery (and please don't link to W3Schools, they are good at SEO and poor at teaching correct information)! – Quentin Sep 27 '10 at 12:38
  • @David, so isn't using the `decodeURIComponent` function a correct approach? – Darin Dimitrov Sep 27 '10 at 12:43
  • It should be the correct approach, but the W3School link really should go. Nick linked to a MDC article, which should be better – Yi Jiang Sep 27 '10 at 12:45
  • — Tongue in cheek, the question specified jQuery. I'm serious about W3Schools though. – Quentin Sep 27 '10 at 12:54
  • 14
    Well I dont see the point to use jQuery when a native javascript function can do it. And what's matter about W3Schools? is not a place where you are going to ask a COMPLEX specific question about programming but at least works as reference or introduction for an general problem. I don't see any bad with that. – ncubica Mar 11 '12 at 01:30
  • @nahum You can find an explanation about some of the problems with W3Schools [here](http://w3fools.com/). – some Oct 21 '12 at 21:48
  • @some well I'm not saying that you should go and learn how to program using W3school, Im just saying as "reference" for easy question and not complex! is quite well. When you have certain good knowledge about the web (programming) sometimes you only need references not to go and learn in that kind of sites, and references can came from any source if are valid, I don't mind if my grand mother suggest me or w3schools :P. – ncubica Oct 23 '12 at 14:52
  • @nahum: I have used W3Schools in the past, and thought it was a great site. But after noticing that some of the important information was missing or dead wrong I had to find something else. I do not recommend w3schools any more. These days I google for the thing I'm searhing for and add MDN, to get pages from Mozilla Developer Network. – some Mar 15 '13 at 22:28
  • @some of course I will never recommend W3C as reference haaha but for me (I already know how works the programming world) if Google show me in any search the first result in W3C and is good, I really don't care... – ncubica Mar 16 '13 at 23:01
  • 4
    @nahum W3C (World Wide Web Consortium) is actually a very good place to get the right information. w3schools, who has NOTHING to do with W3C, isn't. I assume you know the difference between them and that it was just a typo. However, you just demonstrated why they choosed that name and how easy it is to get it mixed up. If you want to continue using them, even though you know that the information they have is misleading or wrong, instead of using something that is easily accessible and with better information, no one will stop you. But you will get complaints when you publicly link to them. – some Mar 31 '13 at 07:56
  • hahaha I see my error you right you can get confuse easily... KIDS "Folks!! don't use W3school at least you know what are you doing" – ncubica Apr 07 '13 at 21:33
  • function is not written by w3school. right function is necessary either it found any site – Himanshu Pandey Aug 01 '13 at 13:31
  • The problem with this answers is, it doesn't answers the "How to do this with jQuery", which is what was asked. You should research for a way to do this trough jQuery and give this answer as an "Extra tip" which works jQuery-less. – LasagnaAndroid Feb 25 '14 at 19:40
  • 3
    @AdriánSalgado, why looking for a function in jQuery when this function already exists in plain javascript? I mean that would be like reinventing the wheel, wouldn't it? Check that out: http://i.stack.imgur.com/ssRUr.gif – Darin Dimitrov Feb 25 '14 at 22:57
  • @DarinDimitrov Haha, yeah I get it, what I'm arguing it's not about "Why use jQuery when it's in vanilla JS?", but what the question is asking for, which explicitly says "How to decode an URL using **jQuery**?", and even if there's a simpler, faster, dependency free solution, the question was asked with jQuery in mind, so the vanilla solution should be given as a "plus", not as the definite answer. Great answer btw, haha. – LasagnaAndroid Feb 26 '14 at 18:06
13

Use decodeURIComponent(), for example:

decodeURIComponent("http%3A%2F%2Fdtzhqpwfdzscm.cloudfront.net%2F4ca06373624db.jpg")

It's not jQuery specific, this is a base JavaScript function.

Nick Craver
  • 623,446
  • 136
  • 1,297
  • 1,155
7

You can simply call the standard javascript functions for encoding and decoding respectively.

encodeURIComponent
decodeURIComponent

Enjoy!

Doug
  • 5,268
  • 24
  • 31
4

If you URL should also contain blanks encoded as '+', the following call will help (taken from https://stackoverflow.com/a/4458580/430742):

decodeURIComponent((str+'').replace(/\+/g, '%20'))
Community
  • 1
  • 1
Jpsy
  • 20,077
  • 7
  • 118
  • 115
1
decodeURIComponent('http%3A%2F%2Fdtzhqpwfdzscm.cloudfront.net%2F4ca06373624db.jpg')
reko_t
  • 55,302
  • 10
  • 87
  • 77