I'm trying to sort out the use of the back button with an AJAX search I built and I'm having a bit of trouble. I'm using localStorage as instructed by this previous question with this code:
// Replace the search result table on load.
if (('localStorage' in window) && window['localStorage'] !== null) {
    if ('myTable' in localStorage && window.location.hash) {
        $("#myTable").html(localStorage.getItem('myTable'));
    }
}
// Save the search result table when leaving the page.
$(window).unload(function () {
    if (('localStorage' in window) && window['localStorage'] !== null) {
        var form = $("#myTable").html();
        localStorage.setItem('myTable', form);
    }
});
I'm having trouble sorting out how to properly clear the localStorage. With the above code the users can perform a search, click a link, click back, and their results will be re-populated. However, if they leave the site and come back later or refresh the search-page their results persist. How can I work around this? Thank you!