Page 2.
<script>
onload = function()
{
    if (window.localStorage.stage == 'one') {
        console.log('1. Got to page 2.  Going to #foo.');
        window.localStorage.stage = 'two';
        // Navigate in a timeout to make sure we create a history entry.
        setTimeout(function() {
            window.location.href = '#foo';
        }, 0);
    } else if (window.localStorage.stage == 'five') {
        console.log('5. Got to page 2 onload.  Success.');
        window.localStorage.stage = 'six';
        history.go(-2);
    }
};

onhashchange = function()
{
    if (window.localStorage.stage == 'two') {
        console.log('2. Got to page 2 #foo.  Going back to start.');
        window.localStorage.stage = 'three';
        history.go(-2);
    }
};

// Go forward to fragment after the page is finished parsing.
document.addEventListener('DOMContentLoaded', function() {
    if (window.localStorage.stage == 'four') {
        console.log('4. Got back to page 2.  Immediately going forward.');
        window.localStorage.stage = 'five';
        history.forward();
    }
});

</script>
<!-- Slow image to let us to go forward while the page is still loading. -->
<img src="slow-resource.pl?delay=100"></iframe>
