Geolocation, Clipboard & Device APIs
Geolocation, Clipboard & Device APIs in Modern JavaScript
Modern web applications can access native device capabilities—including GPS coordinates, the system clipboard, hardware vibration motors, and battery status. These Device APIs enable web apps to provide native-app-like user experiences while respecting user privacy and security sandboxes.
1. The Geolocation API
The navigator.geolocation interface provides access to the device's physical geographic location (GPS, Wi-Fi tri-angulation, cell towers):
2. The Async Clipboard API (navigator.clipboard)
Replacing the legacy, insecure document.execCommand('copy'), the modern Async Clipboard API enables safe reading and writing to the system clipboard with Promise-based asynchronous calls:
3. The Vibration API (navigator.vibrate)
Provides physical haptic feedback on supported mobile devices:
4. The Permissions API: Querying Hardware Status
Before invoking intrusive device features, use the Permissions API to inspect whether permission has already been granted, denied, or needs a prompt:
5. Security Requirements: Secure Contexts (HTTPS)
All modern Device APIs require a Secure Context (HTTPS or localhost) and explicit User Gesture Activation (e.g., must be triggered inside a click event listener). Invoking navigator.clipboard.writeText() automatically on page load without user interaction throws a NotAllowedError.
Practice Quiz
Fullscreen & Screen Orientation APIs
Continue learning with hands-on practice, examples, and exercises in the upcoming topic.
Related Lessons
| Previous Lesson | Next Lesson |
|---|---|
| History API & Client-Side Routing | Fullscreen & Screen Orientation APIs |
Practice Quiz
Test your understanding of this lesson with 5 questions. Each question has one correct answer.