Episode 1 — Fundamentals / 1.16 — Using Browser Functionalities in JavaScript

1.16 — Browser Functionalities: Quick Revision

Compact cheat sheet. Print-friendly.

How to use this material (instructions)

  1. Skim before labs or interviews.
  2. GapsREADME.md1.16.a1.16.d.
  3. Drills1.16-Exercise-Questions.md.
  4. Polish1.16-Interview-Questions.md.

BOM at a glance

ObjectRole
windowGlobal object, viewport, timers, sub-objects
documentDOM root
navigatorBrowser / device / capability info
locationCurrent URL + navigation
historyBack/forward + pushState / replaceState

location

TaskCode idea
Full URLlocation.href
Path + querypathname, search, hash
Navigatelocation.href = url or location.assign(url)
Replace entrylocation.replace(url)
Reloadlocation.reload()

history

history.back();
history.forward();
history.go(-1);
history.pushState(state, "", "/new-path");
history.replaceState(state, "", "/replace");
window.addEventListener("popstate", handler);

Storage

APIScopeLifetime
localStorageOriginUntil cleared
sessionStorageOrigin per tabTab/window
CookiesOrigin + path/domain attrsExpires / max-age / session
localStorage.setItem("k", "v");
localStorage.getItem("k");
JSON.stringify(obj); JSON.parse(str);
document.cookie = "name=value; path=/; max-age=60";

HttpOnly → not readable from JS.


fetch

const res = await fetch(url, { method, headers, body });
if (!res.ok) throw new Error(res.status);
const data = await res.json();
  • Resolved on many HTTP errors — check ok.
  • Rejects mainly on network failure.
  • CORS — cross-origin needs server headers.

Master workflow

  1. BOM — use window / navigator / location / history for environment + navigation.
  2. Persist prefslocalStorage; tab statesessionStorage; HTTP-visible small statecookies.
  3. Networkfetch + ok + parse once + try/catch.
  4. SPA URLpushState + popstate to keep UI in sync.

End of 1.16 quick revision.