While BBQ was nice, it did not entirely solve my problem. For it to work in Chrome / Safari, you need to update the hash before the onbeforeunload event. Part of the requirement however made it so that was the only real time that I could save the state of the page.
I came up with a solution, using both of the other answers here, using pushState and BBQ. I thought I would post it incase anyone else has the same issue I did.
function BackButtonState(saveStateCallback, pageLoadCallback)
{
var executePopStateCounter = null;
var myData = {};
var browserCanPushState = history.pushState ? true : false;
window.onbeforeunload = saveStateCallback;
if (browserCanPushState)
{
//The version of safari this was tested on (5.0.3) uses an outdated version of
//Webkit that has a bug where popstate is not called on the first page load.
//This is a hacky work around until the problem is fixed.
var agt = navigator.userAgent.toLowerCase();
if (agt.indexOf("safari") != -1)
{
executePopStateCounter = setTimeout(pageLoadCallback, 500);
};
window.onpopstate = function(popEvent)
{
clearTimeout(executePopStateCounter);
if (popEvent.state != null)
{
myData = popEvent.state;
}
pageLoadCallback();
}
}
else
{
$(document).ready(pageLoadCallback);
}
this.savePageState = function(state)
{
if (browserCanPushState)
{
history.pushState(state, 'BackButtonState');
}
else
{
$.bbq.pushState(state);
}
}
this.getState = function(item)
{
if (browserCanPushState)
{
return myData[item];
}
else
{
return $.bbq.getState(item);
}
}
this.deserializeSortList = function(sortArrays)
{
var sortList = [];
$.each(sortArrays, function(index, pair)
{
sortList.push([parseInt(pair[0]), parseInt(pair[1])]);
});
return sortList;
}
}
To use this, you do something like:
function saveState()
{
var myData = { bananas: 'are tasty' };
_backButton.saveState(myData);
}
function pageSetup()
{
//Do normal $(document).ready() stuff here
var myOpinionOnFruit = _backButton.getState('bananas');
}
var _backButton = new BackButtonState(saveState, pageSetup);
This is not guarenteed to be bug free, and I am unhappy with the hack I had to do for Safari. I just wanted to post this before I forgot.