← flaviocopes.com

How to set the fragment part of a URL

Published May 02 2022

Psssst! The 2023 WEB DEVELOPMENT BOOTCAMP is starting on FEBRUARY 01, 2023! SIGNUPS ARE NOW OPEN to this 10-weeks cohort course. Learn the fundamentals, HTML, CSS, JS, Tailwind, React, Next.js and much more! ✨

I’ve had the need to programmatically change the fragment part of a URL

That’s the part after the # hash symbol.

I was on index.html and I wanted to change the URL to something like index.html#second.

The reason for this was a bit unusual, but let’s say I had a table of contents but the links weren’t working as I wanted.

Here’s how I did it:

window.location.hash = 'second'


I write books for developers 👇🏼

© 2023 Flavio Copes Flavio Copes made in Italy 🇮🇹 using Notion to Site Notion to Site