Section 24.72. encodeURI( ): escape characters in a URI


24.72. encodeURI( ): escape characters in a URI

ECMAScript v3

24.72.1. Synopsis

 encodeURI(uri) 

24.72.1.1. Arguments

uri

A string that contains the URI or other text to be encoded.

24.72.1.2. Returns

A copy of uri, with certain characters replaced by hexadecimal escape sequences.

24.72.1.3. Throws

URIError

Indicates that uri contains malformed Unicode surrogate pairs and cannot be encoded.

24.72.2. Description

encodeURI( ) is a global function that returns an encoded copy of its uri argument. ASCII letters and digits are not encoded, nor are the following ASCII punctuation characters:

 - _ . ! ~ * ' ( ) 

Because encodeURI( ) is intended to encode complete URIs, the following ASCII punctuation characters, which have special meaning in URIs, are not escaped either:

 ; / ? : @ & = + $ , # 

Any other characters in uri are replaced by converting each character to its UTF-8 encoding and then encoding each of the resulting one, two, or three bytes with a hexadecimal escape sequence of the form %xx. In this encoding scheme, ASCII characters are replaced with a single %xx escape, characters with encodings between \u0080 and \u07ff are replaced with two escape sequences, and all other 16-bit Unicode characters are replaced with three escape sequences.

If you use this method to encode a URI, you should be certain that none of the components of the URI (such as the query string) contain URI separator characters such as ? and #. If the components have to contain these characters, you should encode each component separately with encodeURIComponent( ).

Use decodeURI( ) to reverse the encoding applied by this method. Prior to ECMAScript v3, you can use escape( ) and unescape( ) methods (which are now deprecated) to perform a similar kind of encoding and decoding.

24.72.3. Example

 // Returns http://www.isp.com/app.cgi?arg1=1&arg2=hello%20world encodeURI("http://www.isp.com/app.cgi?arg1=1&arg2=hello world"); encodeURI("\u00a9");  // The copyright character encodes to %C2%A9 

24.72.4. See Also

decodeURI( ), decodeURIComponent( ), encodeURIComponent( ), escape( ), unescape( )




JavaScript. The Definitive Guide
JavaScript: The Definitive Guide
ISBN: 0596101996
EAN: 2147483647
Year: 2004
Pages: 767

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net