Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save scottjehl/788222 to your computer and use it in GitHub Desktop.
Save scottjehl/788222 to your computer and use it in GitHub Desktop.
Check whether hash changes make history entries
// potential test for whether changing location.hash creates a history entry
// in browsers where this isn't true (blackberry, nokia), hash changes can't be reached through the back/forward buttons
// just testing whether this approach is feasible...
var hashMakesHistory = function(){
var histLength = window.history.length,
prevHash = window.location.hash,
historyMade;
//change the hash to something unique
window.location.hash += "*";
//define support bool
historyMade = histLength < window.history.length;
//if it made a history entry, we can go back one to remove the faux entry
if( historyMade ){
history.back();
}
//otherwise, just put things back as they were
else{
window.location.hash = prevHash;
}
return historyMade;
};
@cowboy
Copy link

cowboy commented Jan 20, 2011

Feature detecting hash change support is really problematic. Consider this example, where hash feature detection is done on page A. A -> B -> C works fine, but if you then go back to A, the B -> C forward "history" will get completely overwritten with your test, and the next button will no longer work.

@scottjehl
Copy link
Author

Good point.
What if we run the test once and cookie (or similar) it, that'd be one way to avoid this forward history issue. You can't go "back" to a site you've never been to before, so in theory, we're running that test knowing that there's no forward history entries... right?

@cowboy
Copy link

cowboy commented Jan 20, 2011

Also note that this will return false in IE6/7, even if you're using my hashchange event plugin. That's because the plugin has to poll to detect hash changes, so the new history entry will be added asynchronously.

FWIW, I have considered adding a hash-setting method to allow the hashchange event to work synchronously, but in that case you'd need to call an custom method to set the hash, instead of setting location.hash directly. Either way, it's something to consider.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment