Created
January 20, 2011 17:23
-
-
Save scottjehl/788222 to your computer and use it in GitHub Desktop.
Check whether hash changes make history entries
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
// 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; | |
}; |
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?
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
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.