On this page10 sections
Lecture 028: Charsets & URL Encoding
| Category | 5: Code, Characters & Standards |
| Lecture | 028 of 048 |
| Estimated video length | 10 min |
| Target | HTML5 · any modern browser |
| Level | Beginner |
Computers store text as numbers. A character set is the table that says which number means which letter. Pick the wrong one and "café" turns into "café". URLs have their own rules too: spaces and special characters must be encoded.
Learning Objectives
- Explain what a character set is and why UTF-8 is the standard
- Declare the charset correctly
- Read and write URL-encoded text such as
%20
1. From ASCII to UTF-8
| Charset | Characters | Notes |
|---|---|---|
| ASCII | 128 | English letters, digits, basic punctuation. The original |
| ISO-8859-1 (Latin-1) | 256 | Adds Western European letters such as é and ü |
| UTF-8 | Over 1 million | Every language, plus symbols and emojis. The web standard |
UTF-8 includes all of ASCII, so plain English text is identical in both.
2. Declaring the Charset
Put this as the first thing inside <head>:
<meta charset="UTF-8">Example: English, Urdu, Arabic and emojis on one page:
<meta charset="UTF-8">
<p>English: Hello</p>
<p lang="ur" dir="rtl">اردو: السلام علیکم</p>
<p>Français: café, crème brûlée</p>
<p>Emoji: 🎉 ✅</p>Your editor must also save the file as UTF-8. Most editors do by default; VS Code shows it in the status bar.
3. What Goes Wrong Without It
If a UTF-8 file is read as Latin-1, each multi-byte character falls apart:
café → café
السلام → السلامThese garbled characters are often called "mojibake". The fix is always the same: save as
UTF-8 and declare <meta charset="UTF-8">.
4. URL Encoding
A URL may only contain a limited set of ASCII characters. Anything else is written as %
followed by two hexadecimal digits: the character's byte value.
| Character | Encoded |
|---|---|
| space | %20 (or + in form data) |
! | %21 |
& | %26 |
/ | %2F |
? | %3F |
é | %C3%A9 (two bytes in UTF-8) |
Example: search forms encode for you. Type "tea & cake", submit, and look at the data sent:
<form action="https://www.google.com/search" target="_blank">
<input name="q" value="tea & cake">
<button type="submit">Search Google</button>
</form>The URL becomes …/search?q=tea+%26+cake.
5. Encoding in JavaScript
Example
<p id="out"></p>
<script>
const text = "Lahore & Karachi / 2026";
document.getElementById("out").textContent = encodeURIComponent(text);
</script>encodeURIComponent() encodes; decodeURIComponent() turns it back.
Common Beginner Mistakes
1. <meta charset> too late. Put it first in the <head>, before the title.
2. Spaces in file names. my photo.jpg becomes my%20photo.jpg. Use hyphens instead.
3. Building URLs by hand with special characters. Use encodeURIComponent() for values you insert.
Practice Questions
- What is a character set?
- Which character set should every web page use?
- Where exactly should
<meta charset="UTF-8">go? - What does
%20mean in a URL? - Why does "café" sometimes show up as "café"?
- Which JavaScript function encodes text for a URL?
Practice Answers
- A table that maps numbers stored by the computer to the characters they represent.
- UTF-8.
- As the first element inside
<head>. - A space.
- A UTF-8 file is being read with the wrong character set (such as Latin-1).
encodeURIComponent()
Key Takeaways
- UTF-8 covers every language and emoji; use it everywhere.
- Declare
<meta charset="UTF-8">first in the head, and save files as UTF-8. - URLs encode special characters as
%XX; a space is%20. encodeURIComponent()does the encoding for you.