Created
November 13, 2012 21:15
-
-
Save tedw/4068442 to your computer and use it in GitHub Desktop.
Set URL Hash
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* 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