Entry
How do I encode and decode special characters for and from a link?
How do I encode and decode special characters for and from an URL's ?
How do I encode and decode string characters for a Link/URL ?
Apr 12th, 2000 23:07
Thor Larholm,
Javascript has the escape() and unescape() functions for encoding a
string to and from an URL (Uniform Resource Locator), URI (Uniform
Resource Identifier) or URI-type string.
The escape function encodes special characters in the specified string
and returns the new string. It encodes spaces, punctuation, and any
other character that is not an ASCII alphanumeric character, with the
exception of these characters:
* @ - _ + . /
Example 1. The following example returns "%26":
escape("&") // returns "%26"
Example 2. The following statement returns a string with encoded
characters for spaces, commas, and apostrophes.
escape("The_rain. In
Spain, Ma'am") // returns "The_rain.%20In%20Spain%2C%20Ma%92am"
The string returned by the unescape function is a series of characters
in the ISO-Latin-1 character set.
The escape and unescape methods do not use Unicode as specified by the
ECMA specification.
Examples
The following example returns "&":
unescape("%26")
The following example returns "!#":
unescape("%21%23")
So to correctly call a link with international characters, such as
æ,ø,å,ü, you would use escape() to ensure the browser and the server
understands the link correctly.
location.href = "mypage.html?something=" + escape("æblemåne") // Set's
the location to mypage.html?something=%E6blem%E5ne
If you were to read the string back, you would decode it using unescape
()
something = unescape("%E6blem%E5ne") // Set's something to æblemåne