URL decoding in Javascript

I want to decode a string that has been encoded using the java.net.URLEncoder.encode() method.

I tried using the unescape() function in javascript, but a problem occurs for blank spaces because java.net.URLEncoder.encode() converts a blank space to '+' but unescape() won't convert '+' to a blank space.

Asked by: Jared552 | Posted: 23-01-2022

Answer 1

Try decodeURI("") or decodeURIComponent("") !-)

Answered by: Leonardo579 | Posted: 24-02-2022

Answer 2

Using JavaScript's escape/unescape function is almost always the wrong thing, it is incompatible with URL-encoding or any other standard encoding on the web. Non-ASCII characters are treated unexpectedly as well as spaces, and older browsers don't necessarily have the same behaviour.

As mentioned by roenving, the method you want is decodeURIComponent(). This is a newer addition which you won't find on IE 5.0, so if you need to support that browser (let's hope not, nowadays!) you'd need to implement the function yourself. And for non-ASCII characters that means you need to implement a UTF-8 encoder. Code is available if you need it.

Answered by: Kellan116 | Posted: 24-02-2022

Answer 3

decodeURI[Component] doesn't handle + as space either (at least on FF3, where I tested).

Simple workaround:

alert(decodeURIComponent('http://foo.com/bar+gah.php?r=%22a+b%22&d=o%e2%8c%98o'.replace(/\+/g, '%20'))) 

Indeed, unescape chokes on this URL: it knows only UTF-16 chars like %u2318 which are not standard (see Percent-encoding).

Answered by: Carlos682 | Posted: 24-02-2022

Answer 4


var decoded = decodeURIComponent(encoded.replace(/\+/g," "));

Answered by: Miranda344 | Posted: 24-02-2022

Similar questions

How can I do LZW decoding in Java?

I have a database which contains picture data stored as a binary blob. The documentation says the data is encoded using LZW. I thought that I could decode it using the Zip or GZip input streams found in the Java library, but it didn't work - I got an exception that said the format of the data is not correct. From what I've read, the library uses DEFLATE, which is not LZW. Also, I've read about some licensing proble...

Decoding URI query string in Java

I need to decode a URI that contains a query string; expected input/output behavior is something like the following: abstract class URIParser { /** example input: * something?alias=pos&FirstName=Foo+A%26B%3DC&LastName=Bar */ URIParser(String input) { ... } /** should return "something" for the example input */ public String getPath(); /** should return a map ...

java - Decoding qr code from image stored on the phone with Zxing (on Android phone)

I have an app that receives qr code from the server. I want to decode it (not with intent and camera) and display the text it contains in my app. I have alredy done this in Java SE with jars from zxing with this code: private class QRCodeDecoder { public String decode(File imageFile) { BufferedImage image; try { image = ImageIO.read(imageFile); } catch (IOExcep...

Decoding JSON format in Java

I got a JSON(encoded) format nested Arrays which looks like this; [ [[1234,245,10],[312,234,122],[1234,67788,345],[235,001,332]], [[1234,245,10],[312,234,122],[1234,67788,345],[235,001,332],[1234,67788,3450]], [[1234,245,10],[312,234,122],[1234,67788,345],[235,001,332],[1234,67788,34534]]] So I have one big array which contains three arrays (this can be 2 or more than three arrays sometime...

java - Decoding html returned as json response - android

I am getting following encoded html as a json response and has no idea how to decode it to normal html string, which is an achor tag by the way. x3ca hrefx3dx22http:\/\/wordnetweb.princeton.edu\/perl\/webwn?sx3dstrandx22x3ehttp:\/\/wordnetweb.princeton.edu\/perl\/webwn?sx3dstrandx3c\/ax3e I have tried java.net.UrlDecoder.decode without anyluck.

Decoding Java's JSON Unicode values with PHP

I had experienced different JSON encoded value for the same string depending on the language used in the past. Since the APIs were used in closed environment (no 3rd parties allowed), we made a compromise and all our Java applications are manually encoding Unicode characters. LinkedIn's API is returning "corrupted" values, basically the same as our Java applications. I've already posted a

Decoding a Java/JSON Map into an F# object

I'm having trouble converting a Java/JSON map into a usable F# object. Here's the heart of my code: member this.getMapFromRpcAsynchronously = Rpc.getJavaJSONMap (new Action<_>(this.fillObjectWithJSONMap)) () member this.fillObjectWithJSONMap (returnedMap : JSONMap<string, int> option) = let container = Option.get(returnedMap) let map = container.map for thing in map...

Is there a Java library for decoding php session data?

I am working on an application that mixes php and Java. I need to inject some values into the php session through Java, but to do this I need to be able to parse the values of the php session into a set. Is there a library that will let me undo in Java what session_encode does in php?

java - javax.mail - problem decoding subject

i'm facing a problem decoding a mail with the following subject: Subject: =?ISO-8859-1?Q?Re: Re: Re: Fwd: (GI ?= =?ISO-8859-1?Q?Support-Id:11729)?= javamail decodes it as: =?ISO-8859-1?Q?Re: Re: Re: Fwd: (GI ?= Support-Id:11729) is this a valid subject at all? or should javamail be able to read this? regards

Java UDP server not decoding correct IP address

I am trying to make a client server application using java UDP. when the server takes in the message from the client it should decode the IP address and port number so that it can send back the data. the problem is when the IP address is decoded from the packet is has a / in the front so it cannot return a message. the output for the program is as follows waiting for data RECEIVED: message / 56798 ...

Still can't find your answer? Check out these amazing Java communities for help...

Java Reddit Community | Java Help Reddit Community | Dev.to Java Community | Java Discord | Java Programmers (Facebook) | Java developers (Facebook)