tags:

views:

62

answers:

4

I have a string such as "%2Fu%2F2069290%2F" in JavaScript (extracted from a web page). How do I get the human-readable version of that string?

A: 

Use the unescape() function, eg:

alert(unescape("%2Fu%2F2069290%2F"));
Mike Anchor
+1  A: 
alert(decodeURIComponent("%2Fu%2F2069290%2F"));
Darin Dimitrov
A: 

with unescape() ?

deubeulyou
+5  A: 

Short version: Use decodeURIComponent().

Longer version: In older versions of JavaScript you could use unescape() but that has been deprecated since it only works properly for the LATIN1/ISO8859-1 codeset, so you really want to use decodeURIComponent() which is supported by all modern browsers.

 var c = decodeURIComponent("%2Fu%2F2069290%2F"));
Johan Dahlin
+1 For the additional information about `unescape` and `decodeURIComponent`.
Gumbo
unescape/escape are DOM functions (but not part of any spec) that are supported by some browsers to varying degrees (https://developer.mozilla.org/en/DOM/window.unescape). At least in TraceMonkey, escaping/unescaping non-latin1 codes works fine.decodeURIComponent/encodeURIComponent are core javascript functions supported by all javascript engines, including the engines in browsers (http://www.w3schools.com/jsref/jsref_decodeuricomponent.asp). So best to stick with Johan and Darin's answers.
DaveS