# YUI Browser History Manager: Calendar

This example demonstrates how to use the Browser History Manager to remember which months have been viewed with the calendar widget and dynamically update it when the user presses the browser's back/forward buttons.

## Import the source files and dependencies

In our example, we need the Event Utility, DOM Utility, Calendar Widget, and the Browser History Manager:

<link type="text/css" rel="stylesheet" href="calendar.css"/> <script src="yahoo.js"></script> <script src="event.js"></script> <script src="dom.js"></script> <script src="calendar.js"></script> <script src="history-experimental.js"></script>

## Basic markup

In our example, the calendar widget relies on the following markup:

<div id="calendarContainer"></div>

## Design your application

In our simple example, we have only one module, represented by the calendar widget. We will refer to this module using the identifier "calendar". The state of the calendar module will be represented by a string composed of the month and the year the widget currently renders, separated by `"_"` (e.g. `"2_2007"` for February 2007)

## Retrieve the initial state of the calendar module

Use the `YAHOO.util.History.getBookmarkedState` method and default to the month corresponding to today's date:

var today = new Date(); var defaultCalendarState = ( today.getMonth() + 1 ) + "_" + today.getFullYear(); var bookmarkedCalendarState = YAHOO.util.History.getBookmarkedState( "calendar" ); var initialCalendarState = bookmarkedCalendarState || defaultCalendarState;

## Register the calendar module

Use the `YAHOO.util.History.register` method, passing in the calendar module identifier, the initial state of the calendar module, and the callback function that will be called when the state of the calendar module has changed:

YAHOO.util.History.register( "calendar", initialCalendarState, function( state ) { // Show the right month according to the "state" parameter: calendar.cfg.setProperty( "pagedate", state.replace( "_", "/" ) ); calendar.render(); } );

## Subscribe to the Browser History Manager `onLoad` event

Use the Browser History Manager `onLoad` event handler to instantiate the calendar widget. Also, retrieve the current state of the calendar module, and use that state to show the right month (the current state may be different from the initial state under certain circumstances - see the User's Guide)

var calendar; YAHOO.util.History.onLoadEvent.subscribe( function() { // Retrieve the current state of the calendar module var currentCalState = YAHOO.util.History.getCurrentState( "cal" ); var startDate = { pagedate: currentCalState.replace( "_", "/" ) }; // Instantiate the calendar widget calendar = new YAHOO.widget.Calendar( "calendar", "calendarContainer", startDate ); calendar.beforeRenderEvent.subscribe( handleCalendarBeforeRender, calendar, true ); calendar.render(); } );

## Add history entries

A new history entry must be added every time the user views a new month. Use the calendar widget's `beforeRender` event handler (set to `handleCalendarBeforeRender` - see above):

function handleCalendarBeforeRender() { var calDate = calendar.cfg.getProperty( "pageDate" ); var pageDate = ( calDate.getMonth() + 1 ) + "_" + calDate.getFullYear(); // The following test is crucial. Otherwise, we end up circling forever. // Indeed, YAHOO.util.History.navigate will call the module onStateChange callback, // which will call createCalendar, which will call calendar.render(), which will // end up calling handleCalendarBeforeRender, and it keeps going from here... var currentCalState = YAHOO.util.History.getCurrentState( "cal" ); if ( pageDate != currentCalState ) YAHOO.util.History.navigate( "cal", pageDate ); }

## Initialize the Browser History Manager

Simply call `YAHOO.util.History.initialize` from a script block located right after the opening body tag:

YAHOO.util.History.initialize();
Continue to Functional Example >