Description#
Implement same-document fragment navigation: clicking a link with href="#section" should scroll to the element with id="section" without reloading the page.
Requirements#
Fragment Navigation#
- Clicking
<a href="#id">scrolls to the element with matchingidattribute - Clicking
<a href="#">scrolls to the top of the document - Clicking
<a href="">with no fragment reloads (not fragment nav) - If target
idis not found, try matching<a name="...">for legacy compatibility - If no matching element is found, do nothing (don't scroll)
- Smooth scroll support: respect
scroll-behavior: smoothCSS property (stretch goal)
URL Update#
- Update the URL fragment in the address bar
- Update
location.hash - Push a new history entry for the fragment change
Events#
- Fire
hashchangeevent on the window witholdURLandnewURLproperties hashchangefires AFTER the URL is updated and scroll is performed- Do NOT fire
hashchangefor cross-document navigations (only same-document)
Same-Document Detection#
- Navigation is same-document if the only difference is the fragment
- Same scheme, host, port, path, and query — only fragment differs
pushStatenavigations are also same-document (handled by History API issue)
Acceptance Criteria#
- Clicking
<a href="#id">scrolls to the matching element - URL in address bar updates with the fragment
- New history entry is created for fragment navigation
- Back button returns to previous scroll position
-
hashchangeevent fires with correct oldURL and newURL - No page reload occurs for fragment-only navigations
- Unit tests for fragment matching and hashchange event