HTML and CSS Reference
'Recorded <b>' + times + '</b> clicks';
The doclick() function B is now only responsible for updating the
times variable and changing the hash. The hashchange event c is on the
window object; when it takes place, you check that the hash exists d . In
a real application, you'd also want to check that it had a valid value.
Next, you set the value of times to be the number in the hash e .
Finally, you update the document to reflect the correct page state f .
Let's look at this new code:
As before, the hash in the
URL is updated as the user
But now, when the Back but-
ton is clicked, the onhashchange
function is triggered and the
page state is reset to match
the URL .
The location.hash property and the associated hashchange event are use-
ful if you want to tag particular views of your application and allow the
user to navigate between them. Google Mail uses this approach by
allowing you to navigate between your inbox ( #inbox ), contacts
( #contacts ), and other views—if you have a Gmail account, look at
what happens to the URL as you navigate to various different pages
and then click back.
But as far as state information goes, the hash only lets you store a
string. You could encode a more complex object, but the URL would
quickly become long and unwieldy and wouldn't be memorable for