location
Current page URL information.
screen, location and history expose display information, the current URL and browser navigation state.
const url = new URL(window.location.href);
url.searchParams.set("tab", "settings");
history.pushState({}, "", url);
Browser APIs & BOM
location describes the current URL. You can read the path, query string, hash and origin from it.
history lets JavaScript change the URL and respond to back/forward navigation without a full page reload.
screen exposes device screen information, but layout decisions should usually belong to CSS and responsive design.
Current page URL information.
Structured way to read and change URLs.
pushState, replaceState and popstate navigation.
Screen dimensions and related display information.
Examples
const url = new URL(location.href);
url.searchParams.set("panel", "details");
history.pushState({ panel: "details" }, "", url);
activePanel = "details"; render(); // Reloading or sharing the URL loses the view state.
Code patterns
These examples focus on practical browser APIs: URL state, storage, cookies, clipboard, permissions, observers and offline-capable platform tools.
Use URL for structured parsing.
const url = new URL(window.location.href); console.log(url.pathname);
URLSearchParams handles query strings.
const tab = url.searchParams.get("tab");
Add navigation history entry.
history.pushState({ tab: "settings" }, "", "?tab=settings");
Respond to popstate.
window.addEventListener("popstate", event => {
renderFromUrl();
});
Rules that matter
Browser APIs are powerful because they touch privacy, navigation, storage, permissions and performance. Use them deliberately.
Avoid hand-splitting query strings.
Back button should make sense.
Do not create noisy history entries for tiny fixes.
Back and forward should update the UI.
Shareable state improves usability.
CSS should handle layout adaptation.
Production thinking
Good URL state makes applications shareable, bookmarkable and easier to navigate.
History changes should also update headings, focus and visible context where needed.
Production route handling should keep UI, URL and browser history synchronized.
Clean, crawlable URLs are one of the strongest foundations for discoverable 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