October 5, 2000 - Escaping Separators in mailto: URLs | WebReference

October 5, 2000 - Escaping Separators in mailto: URLs

Yehuda Shiran October 5, 2000
Escaping Separators in mailto: URLs
Tips: October 2000

Yehuda Shiran, Ph.D.
Doc JavaScript

Sometimes you need to substitute characters with their ASCII values, written in a hexadecimal notation. Numbers in hexadecimal notation are written in base 16. A percent sign in the beginning of a number in JavaScript denotes an hexadecimal number. The ASCII code of the Space character, for example, is 32, or %20 in hexadecimal. To convert a two-digit hexadecimal number to decimal, you multiply the left digit by 16 and add it to the right digit (2*16+0=32). Converting characters to hexadecimal is called hex-encoding. Normally, you would want to convert all characters other than letters and numbers. The JavaScript's escape() function does it for you. Let's take, for example, the following script:

var myString = "Read Doc JavaScript's daily tips & biweekly columns!";
var myEncodedString = escape(myString);

The value of myEncodedString will be:


Some applications require you to use hex-encoding. A JavaScript command syntax may call for an ampersand, for example, and thus any ampersand in your original string will cause JavaScript to misinterpret the command. This is the case with the mailto URL. The question mark is used as a delimiter between the mail address and the rest of the URL, and the Ampersand character is used as a delimiter between name/value pairs. If a string includes these characters (? or &), it could not be used as a subject or a body value in a URL. Be sure to encode only the subject and message body values in a mailto URL. Do not encode the delimiters of the URL (? and &). Another way to represent a space is by the + sign. The value Doc JavaScript could appear in the URL as Doc+JavaScript.

Learn more about encoding and decoding URLs in Column 59, IE 5.5: Formatting, URIs, and Stack Operations.