Location, navigation and fragments
Quick answer
Location exposes the current document URL and navigation operations. Changing a URL object is not the same as navigating the active document.
How it works
location.assign(url) requests navigation; location.replace(url) replaces the current history entry rather than keeping it for ordinary Back traversal. location.reload() requests reloading. Changing only a fragment commonly performs same-document navigation. Fragments are not sent as part of an HTTP request target. The examples inspect URL values and current blank-document state without navigating to another origin.
Local examples
Run in a fresh browser document; no external network is needed. The actual ByteMind editor is unverified.
(async () => {const u=new URL("https://example.test/guide#part");u.hash="next";console.log(u.hash,u.pathname);})()Expected console output:
#next /guideA detached URL object changes only its own value.
Run in a fresh browser document; no external network is needed. The actual ByteMind editor is unverified.
(async () => {const before=location.href;const u=new URL("https://example.test/a");u.pathname="/b";console.log(location.href===before,u.pathname);})()Expected console output:
true /bChanging a URL object does not navigate the document.
Common mistakes
Do not pass untrusted destinations directly to location.assign or replace. Navigation methods are effects; test them only on controlled pages.
Guided practice
Explain when an application should use replace instead of assign; test on a controlled origin with two local pages.
Check Your Understanding
Solidify your learning with the interactive assessment directly below this lesson.
Concept Quiz & Questions
Location, navigation and fragments — Knowledge Check
Apply the concept and reason about the example output.
Which Location method replaces the current session-history entry?
Are URL fragments normally transmitted in HTTP request targets?
Does modifying a detached URL object change location.href?
What is the expected console output of Location, navigation and fragments example 1?
(async () => {const u=new URL("https://example.test/guide#part");u.hash="next";console.log(u.hash,u.pathname);})()What is the expected console output of Location, navigation and fragments example 2?
(async () => {const before=location.href;const u=new URL("https://example.test/a");u.pathname="/b";console.log(location.href===before,u.pathname);})()