Lesson 3 of 9
Geolocation & Drag and Drop
Access the user's location with the Geolocation API and create interactive interfaces with the native HTML5 Drag and Drop API. Both require user permission and work across modern browsers.
HTML5 Browser APIs
- Geolocation: navigator.geolocation.getCurrentPosition() - gets latitude/longitude. Requires user permission. GPS on mobile, IP-based on desktop
- Drag and Drop: draggable='true' on elements. Events: dragstart, dragover, drop. Use dataTransfer to pass data between drag source and drop target
- Intersection Observer: Lazy load images and trigger animations when elements enter the viewport. More performant than scroll event listeners
- Web Workers: Run JavaScript in background threads. Cannot access DOM but handle heavy computation without blocking the UI
- Service Workers: Proxy between browser and network. Enable offline functionality, push notifications, and background sync
- Clipboard API: navigator.clipboard.writeText() for programmatic copy. Replaces the old document.execCommand('copy')
Geolocation Code
// Geolocation API
if ('geolocation' in navigator) {
navigator.geolocation.getCurrentPosition(
(position) => {
const { latitude, longitude, accuracy } = position.coords;
console.log(`Lat: ${latitude}, Lng: ${longitude}`);
console.log(`Accuracy: ${accuracy} meters`);
},
(error) => {
switch (error.code) {
case error.PERMISSION_DENIED:
console.log('User denied location access');
break;
case error.POSITION_UNAVAILABLE:
console.log('Location unavailable');
break;
case error.TIMEOUT:
console.log('Request timed out');
break;
}
},
{ enableHighAccuracy: true, timeout: 10000, maximumAge: 300000 }
);
}
// Drag and Drop
const draggable = document.getElementById('item');
const dropzone = document.getElementById('zone');
draggable.addEventListener('dragstart', (e) => {
e.dataTransfer.setData('text/plain', e.target.id);
e.target.style.opacity = '0.5';
});
dropzone.addEventListener('dragover', (e) => {
e.preventDefault(); // Required to allow dropping
e.currentTarget.style.background = '#e8f5e9';
});
dropzone.addEventListener('drop', (e) => {
e.preventDefault();
const id = e.dataTransfer.getData('text/plain');
const element = document.getElementById(id);
e.currentTarget.appendChild(element);
element.style.opacity = '1';
});Pro Tip
Always provide a fallback when using the Geolocation API. Check for support with 'geolocation' in navigator before calling getCurrentPosition(). Handle all three error codes (PERMISSION_DENIED, POSITION_UNAVAILABLE, TIMEOUT) gracefully - show a zip code input form as a fallback for users who deny location access.
HTML5 native Drag and Drop enables file drag uploads and reorderable lists
Quick Quiz: Browser APIs
Key Takeaways
- Access the user's location with the Geolocation API and create interactive interfaces with the native HTML5 Drag and Drop API.
- Geolocation: navigator.geolocation.getCurrentPosition() - gets latitude/longitude. Requires user permission. GPS on mobile, IP-based on desktop
- Drag and Drop: draggable='true' on elements. Events: dragstart, dragover, drop. Use dataTransfer to pass data between drag source and drop target
- Intersection Observer: Lazy load images and trigger animations when elements enter the viewport. More performant than scroll event listeners
Finished this lesson?