Is there a way to respond to the back button being hit (or backspace being pressed) in javascript when only the location hash changes? That is to say when the browser is not communicating with the server or reloading the page.
Google found this
http://www.hunlock.com/blogs/Mastering_The_Back_Button_With_Javascript
Sorry, misunderstood, is this what you mean
The suggestion in the linked site is to add a click event on all links to check if the hash is being changed. They also suggest polling (as has been suggested by others here). Apparently IE8 has direct support for it.
onLocationChange
may also be useful. Not sure if this is a Mozilla-only thing though, appears that it might be.
I think you'll just have to use setInterval
to poll the state of window.location.hash
regularly.
var hash = location.hash;
setInterval(function()
{
if (location.hash != hash)
{
alert("Changed from " + hash + " to " + location.hash);
hash = location.hash;
}
}, 100);
It's not instantaneous, but it feels responsive enough to me.
Did you took a look at this? http://developer.yahoo.com/yui/history/
I have created a solution which may be of use to some people. http://www.bajb.net/2010/02/browser-back-button-detection/. Simply include the code on your page, and you can write your own function that will be called when the back button is clicked.
I have tested in IE, FF, Chrome, and Safari, and are all working. The solution I have works based on iframes without the need for constant polling, in IE and FF, however, due to limitations in other browsers, the location hash is used in Safari.