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" />

$("#hash_link_input").keyup(function(){
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.

It's only fair to share...Tweet about this on TwitterShare on FacebookEmail this to someoneShare on Google+
This entry was posted in JavsScript, jQuery and tagged , , , , . Bookmark the permalink.