geolocation
Requests location with user permission.
Geolocation and Notifications are permission-based APIs. They can be useful, but they must be requested with restraint.
if ("geolocation" in navigator) {
// Ask only after a clear user action.
}
const permission = await Notification.requestPermission();
Browser APIs & BOM
Geolocation can request the user position. Notifications can ask permission to show system-level notifications.
Both APIs are sensitive. Asking too early or without context damages trust and can reduce permission acceptance.
Always provide a clear reason, a user action and a fallback when permission is denied or the API is unavailable.
Requests location with user permission.
Requests notification permission and displays notifications.
Browser-controlled user decision.
A non-permission path when users decline.
Examples
button.addEventListener("click", () => {
helpText.textContent = "We use location only to show nearby results.";
navigator.geolocation.getCurrentPosition(onSuccess, onError);
});
navigator.geolocation.getCurrentPosition(onSuccess); Notification.requestPermission();
Code patterns
These examples focus on practical browser APIs: URL state, storage, cookies, clipboard, permissions, observers and offline-capable platform tools.
Do not assume it exists.
if ("geolocation" in navigator) {
enableLocationButton();
}
Do not ask on page load.
button.addEventListener("click", () => {
navigator.geolocation.getCurrentPosition(handlePosition, handleError);
});
Ask only when notifications make sense.
const result = await Notification.requestPermission(); if (result === "granted") showNotification();
Let users continue without permission.
if (permission !== "granted") {
showInPageReminder();
}
Rules that matter
Browser APIs are powerful because they touch privacy, navigation, storage, permissions and performance. Use them deliberately.
Permission prompts should follow a clear user action.
Users should know why the API is needed.
Denied permission is a normal path.
The feature should degrade gracefully.
Respect the user decision.
Request only what the feature truly needs.
Production thinking
Permission APIs are trust contracts with users.
Permission-dependent features should still have readable fallback controls and messages.
Production permission flows need analytics, fallbacks and careful wording.
Permission prompts on page load can harm user experience and should not block content.
Live code lab
The preview runs inside an isolated iframe. The JavaScript is placed inside the HTML editor for now, so every example stays together and remains easy to understand.
Mini assignment
Practice assignment
Try it yourself
Self-check
If you can explain support, permissions, fallback and cleanup, you are ready to use the API responsibly.
Senior audit upgrade
Last reviewed: 11 June 2026. Geolocation and notifications require user permission. A denied prompt is a normal state, not an error surprise.
Use these references when browser support, syntax details or proposal status matters.