home | O'Reilly's CD bookshelfs | FreeBSD | Linux | Cisco | Cisco Exam  


JavaScript: The Definitive Guide

Previous Chapter 21
JavaScript Reference
Next
 

escape() Function

Name

escape() Function---encode a string for transmission

Availability

Navigator 2.0, Internet Explorer 3.0

Synopsis

escape(s)

Arguments

s

The string that is to be "escaped" or encoded.

Returns

An encoded copy of s.

Description

The escape() function is a built-in part of JavaScript; it is not a method of any object.

escape() creates and returns a new string that contains an encoded version of s. The string s itself is not modified.

The string is encoded as follows: all spaces, punctuation, accented characters, and any other characters that are not ASCII letters or numbers are converted to the form %xx, where xx is two hexadecimal digits that represent the ISO-8859-1 (Latin-1) encoding of the character. For example, the ! character has the Latin-1 encoding of 33, which is 21 hexadecimal, so escape() replaces this character with the sequence %21. Thus the expression:

escape("Hello World!");

yields the string:

Hello%20World%21

The purpose of the escape() encoding is to ensure that the string is portable to all computers, and transmittable across all networks, regardless of the character encodings the computers or networks support (as long as they support ASCII, however).

The encoding performed by escape() is like the standard URL-encoding used to encode query strings and other portions of a URL that might include spaces, punctuation, or characters outside of the standard ASCII character set. The only difference is that in the URL encoding, spaces are replaced with a `+' character, while escape() replaces spaces with the %20 sequence.

Usage

Use the unescape() function to decode a string encoded with escape().

A common use of escape() is to encode cookie values, which have restrictions on the punctuation characters they may contain. See "Document.cookie".


Previous Home Next
Embed Book Index eval()

HTML: The Definitive Guide CGI Programming JavaScript: The Definitive Guide Programming Perl WebMaster in a Nutshell