Going Further Lesson 39 of 42
HTML Web APIs — Web Storage, Geolocation and More
Discover the built-in browser features you can use from a web page, like saving data with localStorage, finding the user's location, and copying to the clipboard.
Modern browsers come with many built-in tools that your pages can use through JavaScript. These tools are called Web APIs. API stands for Application Programming Interface, which is a fancy way of saying "a set of commands a program can use to ask for something." You've already used one: document.getElementById is part of the DOM API.
This lesson introduces the APIs that beginners use most. You don't need to memorize them. The goal is to know what's possible.
Web Storage: remembering things
Normally, when you close a page, everything typed on it is forgotten. localStorage lets your page save small pieces of text in the browser that stay there even after the browser is closed and the phone is restarted.
It works like a labeled box: you save a value under a name (the key), and later you get it back with the same name.
localStorage.setItem("name", "Juan"); // save
localStorage.getItem("name"); // read → "Juan"
localStorage.removeItem("name"); // delete
<p id="hello">Hello, stranger!</p>
<label>Your name: <input id="nameBox"></label>
<button type="button" onclick="saveName()">Save</button>
<button type="button" onclick="forget()">Forget me</button>
<script>
// The try...catch shows a message instead of crashing when storage is blocked.
function show() {
let name;
try { name = localStorage.getItem("demo-name"); }
catch (e) { document.getElementById("hello").innerHTML = "⚠️ Storage is blocked in this practice frame. Save the code as an .html file and open it in your browser."; return; }
document.getElementById("hello").innerHTML = name ? "Welcome back, " + name + "! 👋" : "Hello, stranger!";
}
function saveName() {
try { localStorage.setItem("demo-name", document.getElementById("nameBox").value); } catch (e) {}
show();
}
function forget() {
try { localStorage.removeItem("demo-name"); } catch (e) {}
show();
}
show();
</script>
Save your name, then reload the page. The greeting remembers you. This is exactly how this website remembers which lessons you've finished: the checkmarks in the sidebar are saved in your browser's localStorage.
localStorage vs. sessionStorage
localStorage— stays until it is deleted (by your code or by clearing browser data).sessionStorage— same commands, but forgotten when the tab is closed.
Geolocation: where am I?
The Geolocation API can ask for the user's location, using GPS, Wi-Fi or the mobile network. The browser always asks permission first. A pop-up appears, and nothing is shared unless the user taps "Allow".
navigator.geolocation.getCurrentPosition(
function (pos) {
console.log(pos.coords.latitude, pos.coords.longitude);
},
function (error) {
console.log("Location not allowed or not available.");
}
);
The first function runs if it works, and gets the latitude and longitude. The second runs if the user says no, or the location can't be found. Delivery apps, "find the nearest branch" pages and weather sites use this.
Clipboard: copy with one click
The Clipboard API lets a button copy text, the way the "Copy" buttons on this site's examples do.
<p>Reference No.: <strong id="ref">1029 384 756</strong></p>
<button type="button" onclick="copyRef()">Copy</button>
<span id="status"></span>
<script>
function copyRef() {
const text = document.getElementById("ref").textContent;
navigator.clipboard.writeText(text)
.then(() => document.getElementById("status").textContent = " Copied ✓")
.catch(() => document.getElementById("status").textContent = " Copy is blocked in this practice frame — it works on a real page.");
}
</script>
Other APIs you'll hear about
| API | What it does | Example use |
|---|---|---|
| Drag and Drop | Lets users drag elements and files | Uploading a photo by dragging it onto the page |
| Fetch | Gets data from a server without reloading | Showing live prices or weather |
| Notifications | Shows system notifications (with permission) | "Your order is ready!" |
| Web Speech | Speech recognition and text-to-speech | Voice search |
| Vibration | Makes a phone vibrate | Feedback in a game (Android) |
| Web Workers | Runs heavy code in the background | Processing a big file without freezing the page |
| Service Workers | Lets a site work offline | Reading saved lessons with no data connection |
Permissions and respect
Powerful APIs like location, camera, microphone and notifications always need the user's permission. Ask only when it's clearly needed, and explain why. For example, show a button "Find the nearest branch" instead of asking for location as soon as the page loads. People trust pages that ask politely, and in the Philippines, the Data Privacy Act of 2012 (RA 10173) also requires that personal data be collected only for a clear purpose.
Quick Quiz
Check what you learned. Walang grade ito, practice lang. 🙂
By Sir Reyel · Last updated 2026-12-04