Change URL hash with JavaScript using location.hash

This fun little function will change the hash (#) in the URL.

In this example, I have an input element and it’s contents will populate the hash.

On the “keyup” event the “location.hash” function creates or changes the hash to the value that is typed into the text box.

<input id="hash_link_input" type="text" />

location.hash = $(this).val();

This could be helpful if you have a long page and you have anchor links associated with sections of the page, and you want the hash to change depending on what section of the page you’ve scrolled to.

Thank you for reading!

It's only fair to share...Tweet about this on Twitter
Share on Facebook
Email this to someone