Skip to content

Instantly share code, notes, and snippets.

@tedw
Created November 13, 2012 21:15
Show Gist options
  • Save tedw/4068442 to your computer and use it in GitHub Desktop.
Save tedw/4068442 to your computer and use it in GitHub Desktop.
Set URL Hash
/* Set hash */
var hashSet = function( value ) {
value = value || '';
// If no value and no hash, don't bother updating
if ( value || location.hash ) {
// Use pushState if possible to prevent page jump
if ( Modernizr.history ) {
// console.log('Push: ' + ( value ? '#' + value : '/' ) );
history.pushState(null, null, ( value ? '#' + value : '/' ) );
}
else {
// console.log('Hash update: ' + ( value ? value : '!' ) );
// Instead of removing hash, use bang to prevent jump
location.hash = ( value ? value : '!' );
}
}
};
// Clear any unofficial params added to the URL, e.g. Facebook "post_id"
if ( window.location.href.indexOf('?') > -1 ) {
hashSet();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment