Playing with hash tags in url with javascript – add, remove, and modify hash tags

Playing with hash tags in url with javascript – add, remove, and modify hash tags

Sometimes its fun and beneficial to use hash tags in urls (#modify) instead of GET values (?action=modify). Hash values can be added and modified, so the values can be changed while the user interacts with the page. jQuery isn’t really useful in this case, since we’re just using native javascript for this…it’s simple enough :)

Example of getting hash value

var hash = window.location.hash.substring(1);
  if (hash == 'modify') {
    // react on hash value
  }

To modify or remove the hash value while on a page

window.location.hash = 'changed';
// or to remove
window.location.hash = '';

Comments are closed.