having fun with code

Modifying the URL Hash without affecting the browser history

Although this is not very common, some times you would like to modify the URL hash (this is the value after the # on the URL) without affecting the browser history. Let’s say you have multiple sets of tabs on your page or simply that your hash is going to change very often. But still want the user to be able to grab a permanent link to that page configuration.

The trick is to use the replace function, instead of directly assigning the value.

  1. // Direct assignment will add a new step to the history on browsers like Firefox 3
  2. window.location.hash = newHash;
  3. // Using the replace function changes the hash without affecting the browser history.
  4. window.location.replace(window.location.href.split('#')[0] + '#' +newHash);

Nice.

Related Posts:

Leave a Reply

You can use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

Additional comments powered by BackType

About the blog

This is a blog about development, focused mainly on Javascript but also other languages like python, shell scripts and more.

About the author

Eneko Alonso is a software engineer and UI developer with more than eight years of experience in software and web development. He lives in San Luis Obispo, California and works at LEVEL Studios.

Contact Info

Contact Info