advertisement -- please support sponsors

bugs, compatibility, forms:
fixing the unescape () function

The purpose of JavaScript's unescape () function is to unencode URL-encoded strings. For example, the expression

unescape ("It%27s%20hard%20to%20read%21")
returns the string "It's hard to read!". The companion to the unescape () function is the escape () function, which does the opposite: it URL-encodes unencoded strings. Together, these functions provide a framework for manipulating search strings that commonly follow form-generated URLs.

Look again at the unescape () example given in the preceding paragraph. You will notice that the substring "%20" is the encoded equivalent of the space character. Although some browsers may encode the space character with "%20", the more prevalent encoding method for spaces is to replace them with "+" (plus) characters. (Next time you perform a multi-word search on Alta VistaTM, for example, take a look at the location bar and you will see what I mean.)

Therefore, the unescape () function is obligated to give the same results for

unescape ("It%27s+hard+to+read%21")
as it does for
unescape ("It%27s%20hard%20to%20read%21") .
Unfortunately, Netscape's current implementation of unescape () does not live up to this standard. Instead, the expression with the pluses returns an incorrect result: "It's+hard+to+read!" Based on what I have observed from the beta releases, it looks like this problem will persist in Netscape 4.

A possible work-around is to use an alternate function, myunescape (), which I have written to overcome the shortcomings of the original escape () function. It is given in the edit box below so that you can cut and paste it into your own JavaScript programs if you need it.

The myunescape () function works by converting all occurrences of "+" in the input string to "%20". The resulting string is then handed over to the original unescape () function for processing, and the correct result is returned.

the escape laboratory

A laboratory is provided below to help you experiment with the various effects of the escape (), unescape (), and myunescape () functions. Just for fun, try entering the string "It%27s+hard+to+read%21" and see which unescape function does the job right!

Enter some text in the box below
and select a function to transform it.

Charlton Rose
June 7, 1997