Lesson 8 of 11
Clipboard & Geolocation API
Modern browser APIs give JavaScript access to system features - the clipboard for copy/paste and geolocation for user position. These are permission-based APIs that require user consent.
Clipboard & Geolocation
- navigator.clipboard.writeText(text) - Copy to clipboard (async, requires HTTPS)
- navigator.clipboard.readText() - Read from clipboard (needs permission)
- navigator.geolocation.getCurrentPosition(success, error) - Get user's location
- Position object - coords.latitude, coords.longitude, coords.accuracy
- Error handling - User may deny permission. Always handle errors
- Both require permission - Browser prompts user to allow. Always provide fallbacks
Clipboard & Geolocation Code
// Clipboard API - copy text
async function copyToClipboard(text) {
try {
await navigator.clipboard.writeText(text);
console.log("โ
Copied to clipboard:", text);
} catch (err) {
console.error("โ Copy failed:", err);
// Fallback for older browsers
const textarea = document.createElement("textarea");
textarea.value = text;
document.body.appendChild(textarea);
textarea.select();
document.execCommand("copy");
textarea.remove();
}
}
// copyToClipboard("Hello, World!");
// "Copy code" button
// const codeBlock = document.querySelector("pre code");
// const copyBtn = document.querySelector(".copy-btn");
// copyBtn.addEventListener("click", () => {
// copyToClipboard(codeBlock.textContent);
// copyBtn.textContent = "Copied! โ
";
// setTimeout(() => copyBtn.textContent = "Copy", 2000);
// });
// Geolocation API
function getLocation() {
if (!navigator.geolocation) {
console.log("Geolocation not supported");
return;
}
navigator.geolocation.getCurrentPosition(
(position) => {
const { latitude, longitude, accuracy } = position.coords;
console.log(`๐ Location: ${latitude}, ${longitude}`);
console.log(`Accuracy: ${accuracy} meters`);
// Use with maps API: show on map
},
(error) => {
console.error("Location error:", error.message);
// User denied or location unavailable
},
{ enableHighAccuracy: true, timeout: 5000 }
);
}
// getLocation();
console.log("Clipboard & Geolocation API demonstrated");Tip
Tip
Always check for API support before using it: if ('clipboard' in navigator) { }. Not all browsers support all APIs, and some require HTTPS. Gracefully degrade when APIs are unavailable.
Clipboard & Geolocation API in JavaScript
Common Mistake
Warning
Requesting permissions without user interaction. Browser APIs like geolocation and clipboard require the request to originate from a user gesture (click, keypress). Calling navigator.clipboard.writeText() on page load gets denied.
Practice Task
Note
Browser APIs: (1) Build a 'Copy to clipboard' button with visual feedback. (2) Get the user's location and display it. (3) Use the Notification API to show a desktop notification (with permission request).
Quick Quiz
Key Takeaways
- Modern browser APIs give JavaScript access to system features - the clipboard for copy/paste and geolocation for user position.
- navigator.clipboard.writeText(text) - Copy to clipboard (async, requires HTTPS)
- navigator.clipboard.readText() - Read from clipboard (needs permission)
- navigator.geolocation.getCurrentPosition(success, error) - Get user's location
Finished this lesson?