escape() (Function/global)

URL escaping a text string.

Availability:

ECMAScript edition - 2
JavaScript - 1.0
JScript - 1.0
Internet Explorer - 3.02
Netscape - 2.0
Netscape Enterprise Server - 2.0
Opera - 3.0
Deprecated
Property/method value type:String primitive
JavaScript syntax:-escape(anInputString)
-escape(anInputString, aSwitch)
Argument list:anInputStringA string of un-escaped (normal) characters
aSwitchA switch that allows plus signs to be encoded or not

The escape() function computes a new version of the string value it has passed. The new version has certain characters replaced with hexadecimal escape sequences.

All character codes from zero to 32 (decimal) will be escaped.

All character codes above 126 will be escaped.

The table summarizes the characters below 127 that will be escaped.

EscapeCharacter
%09Tab
%0ALine feed
%0DCarriage return
%20space
%21!
%22"
%23#
%24$
%25%
%26&
%27'
%28(
%29)
%3A:
%3B;
%3C<
%3D=
%3E>
%3F?
%5B[
%5C\
%5D]
%5E^
%60`
%7B{
%7C|
%7D}
%7E~
%7FDelete

For those characters that are replaced whose Unicode encoding is 0xFF or less, a two digit escape sequence of the form %xx is used. For those characters that are replaced whose Unicode character value is greater than 0xFF, a four-digit escape sequence of the form %uxxxx is used.

The encoding is partly based on the encoding described in document RFC1738. However the entire coding scheme goes beyond the scope of that RFC document.

Most non alphanumeric characters will be escaped. All control codes are and also the upper 128 of the 255 character codes that are normally used in a web browser will be escaped too. The example script generates a table of character codes that show how they are escaped. Note that control characters will be represented by a 'missing character' box, but on some platforms, special characters are mapped for display in these lower code points (0-31).

MSIE version 4 introduces Unicode support.

Netscape supports the optional second parameter to switch on the encoding of plus signs. You should place an integer 1 in this argument to activate plus encoding.

As far as ECMAScript is concerned, this is superceded in edition 3 with a set of generalized URI handling functions. The JScript 5.5 documentation refers to the escape() function as a deprecated feature.

Example code:

   // Create a 0-255 lookup table of escapes

   document.write("<TABLE BORDER=1>");

   document.write("<TR><TH>Index</TH>");

   document.write("<TH>Char</TH>");

   document.write("<TH>Escape</TH></TR>");

   for(ii=0; ii<256; ii++)

   {

      myBinary        = ii.toString(2);

      myBinaryPadding = "00000000".substr(1,(8-myBinary.length));

   

      myOctal         = ii.toString(8);

      myOctalPadding  = "000".substr(1,(3-myOctal.length));

   

      myHex           = ii.toString(16);

      myHexPadding    = "00".substr(1,(2-myHex.length));

   

      myChar          = String.fromCharCode(ii);

   

      document.write("<TR ALIGN=RIGHT><TD>");

      document.write(ii);

      document.write("</TD><TD>");

      document.write("&nbsp;"+myChar);

      document.write("</TD><TD>");

      document.write(escape(myChar));

      document.write("</TD></TR>");

   }

   document.write("</TABLE>");

See also:Cast operator, decodeURI(), decodeURIComponent(), encodeURI(), encodeURIComponent(), Function property, Global object, unescape(), URI handling functions

Cross-references:

ECMA 262 edition 2 - section - 15.1.2.4

ECMA 262 edition 3 - section - B.2.1

ftp://ftp.isi.edu/in-notes/