WEEK: 6
Active: Not Currently Active
Work Due:

Escape Characters

As you may have discovered, HTML specifies that certain are “reserved” or serve a special purpose for browsers. The most obvious conflicts are the greater than and less than characters (<, >), since these identify tags to browsers in HTML. Any character that may be accidentally interpreted as markup by the browser should be replaced with its escape code equivalent. There are also characters that have no easy “keyboard” equivalent, (such as the copyright symbol - © ) in which case an escape code must be used.


Question: So what is a developer to do if they need to write some math (or something else less stressful than including comparison operators in their page)?

Answer: Use special codes, known as escape characters, that the browser can use to render the intended character!


This is not a fun process, but one that is necessary since HTML is a language.

NOTE: Also, for those of you who are more “security” minded or wanting to protect yourself and your client from “middle-man” attacks, using escape characters is critical, as evidence by some discussions around the web.

How-To

Escape codes always start with an ampersand (&) character followed by the individual code and finished with a semicolon (;).

Character Code

All characters have a numerical, decimal based (in addition to hexadecimal), value. In HTML, prepend decimal this value with the ampersand identifier as well as a number sign (&#).

Using escape character codes in HTML text is straight forward. Simply replace the character you want with its escape code equivalent in the text.

For example, we could write the word “CAT”, by wrapping the individual letter’s decimal values (67, 65, and 84) in the escape code values as;

<h1>Where is the &#67;&#65;&#84; ?</h1>

Where is the CAT?

Entity Name

There are two ways of including an escape character in your HTML text. The first, mentioned above, simply utilizes the decimal based character code. This of course can be difficult to remember for the characters you may need.

Characters used often have special codes that are more “human readable”, known as “entity names”. For example, the less than and greater than characters (< & >) have entity names ‘lt’ and ‘gt’, respectively. To use an entity name, prepend it with the ampersand and appended with a semicolon. So to use < & > as actual characters in HTML write &lt; and &gt;.

Common Escape Character Codes

These are some of the more commonly used escape code characters, along with their entity name and decimal code.

Symbol Name Entity Name Code
© Copyright &copy; &#169;
® Registered trademark &reg; &#174;
Trademark &trade; &#8482;
< Less than &lt; &#60;
> Greater than &gt; &#62;
& Ampersand &amp; &#38;
" Quotation mark &quot; &#34;
¢ Cent &cent; &162;
£ Pound &pound; &163;
¤ Currency &curren; &164;
¥ Yen &yen; &165;
Euro &euro; &8364;

Resources

{ TODO: }

  • Read pgs 193-194 in the Duckett.

Previous section:
Next section: