Availability: |
| |||||||||
Property/method value type: | String primitive | |||||||||
JavaScript syntax: | - | escape(anInputString) | ||||||||
- | escape(anInputString, aSwitch) | |||||||||
Argument list: | anInputString | A string of un-escaped (normal) characters | ||||||||
aSwitch | A 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.
Escape | Character |
%09 | Tab |
%0A | Line feed |
%0D | Carriage return |
%20 | space |
%21 | ! |
%22 | " |
%23 | # |
%24 | $ |
%25 | % |
%26 | & |
%27 | ' |
%28 | ( |
%29 | ) |
%3A | : |
%3B | ; |
%3C | < |
%3D | = |
%3E | > |
%3F | ? |
%5B | [ |
%5C | \ |
%5D | ] |
%5E | ^ |
%60 | ` |
%7B | { |
%7C | | |
%7D | } |
%7E | ~ |
%7F | Delete |
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.
// 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(" "+myChar); document.write("</TD><TD>"); document.write(escape(myChar)); document.write("</TD></TR>"); } document.write("</TABLE>");
Prev | Home | Next |
Escape sequence (\) | Up | Escaped JavaScript quotes in HTML |
JavaScript Programmer's Reference, Cliff Wootton Wrox Press (www.wrox.com) Join the Wrox JavaScript forum at p2p.wrox.com Please report problems to support@wrox.com © 2001 Wrox Press. All Rights Reserved. Terms and conditions. |