Java Script 1.5 also provides the encodeURIComponent function, which can be used to encode part of a URI. For example, if you created an identifier containing a slash, neither the escape function nor the encodeURI will encode slashes because they are a legal characters in a URL or URI.
Welcome to Bucaro TecHelp!

Welcome to Bucaro TecHelp!
Maintain Your Computer and Use it More Effectively
to Design a Web Site and Make Money on the Web

[About BTH]  [User Agreement]  [Privacy Policy]  [Site Map]  [Contact Form]  [Advertise on BTH]  [News Feed]

Google
Web
This Site

Java Script Character Encoding and Decoding

You may have already discovered that the address bar in your web browser won't accept a character string with a space in it. To get the character string to work, you need to replace the space with something. That something is the hexadecimal code for a space %20.

In addition to not accepting spaces, other characters have meaning in a URL (Uniform Resource Locator). For example a question mark needs to be replaced with %3F, an ampersand needs to be replaced with %26, and an equal sign needs to be replaced with %3D.

Fortunately, you don't have to perform these replacements manually, Java Script provides several functions that will automatically replace the necessary characters in a string with their hexadecimal code equivalents. The Java Script escape function returns a string with all spaces and non-alphanumeric characters replaced with their hexadecimal code equivalents.

Click on the link below to see an example.

escape('!@#$%^&*(){}[]=:;?+./')

The escape function was designed for URLs (addresses to web resources). The W3C now views the Web as a set of named objects accessed by URIs (Uniform Resource Identifier). Some object names use characters that are escaped by the escape function, so in Java Script 1.5 (ECMAScript 3.0) and higher, the escape function has been deprecated in favor of the encodeURI function.

Java Script 1.5 also provides the encodeURIComponent function, which can be used to encode part of a URI. For example, if you created an identifier containing a slash, neither the escape function nor the encodeURI will encode slashes because they are a legal characters in a URL or URI.

You could encode it with the encodeURIComponent function, But you wouldn't use the encodeURIComponent function to encode an entire URI because it would replace the directory delimiter slashes with their hexadecimal code equivalents.

Which to use; encodeURI, encodeURIComponent, or escape?

escape does not encode these characters: @*/+. Since the + and . characters have meaning in a URI, escape is not useful for encoding URIs. encodeURI does not encode these characters: !@#$&*()=:/;?+' Since the & + and ? characters are used in query strings, encodeURI is not useful for encoding URLs.

Click on the link below to see an example.

encodeURI('!@#$%^&*(){}[]=:;?+./')

Web Design Sections

RSS Feed RSS Feed

Java Script Quick Reference
Java Script Data Types
Java Script Reserved Words
Java Script Arithmetic Operators
Comparison Operators
Java Script Arrays
Java Script Character Encoding and Decoding
The if/else Structure
The switch/case Structure
The for Loop
The while Loop
The break Statement
The continue Statement
JavaScript Math Object
Round a Number
Determine Absolute Value
Determine Minimum and Maximum
Generating Random Numbers
Java Script Trigonometric Methods
Java Script Number Object
Format a Number as Currency
Java Script Strings
Compare Two Strings
Find a Character or a Substring Within a String
Include a Quote Character in a String
Include a Backslash Character in a String
Define Lines in a String
Use Escape to Replace Dangerous Characters
Convert a Number to a String
Convert a String to a Number
The Document Object Model (DOM)
Accessing Web Page Elements
Interactively Set Webpage Colors
Get Webpage File Date and File Size
Dueling Windows
Cookie Power Made Easy
Web Designer's Reference

[Site User Agreement]  [Advertise on This site]  [Search This Site]  [Contact Form]
Copyright©2001-2007 Bucaro TecHelp P.O.Box 18952 Fountain Hills, AZ 85269