YUI Library Home

Site Search (YDN & YUIBlog):

# YUI Library Examples: Browser History Manager: Calendar Control

# Browser History Manager: Calendar Control

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.

View example in new window.

## Basic markup

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

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

## Add the markup required by the Browser History Manager

<iframe id="yui-history-iframe" src="assets/blank.html"></iframe> <input id="yui-history-field" type="hidden">

This markup should be inserted right after the opening `body` tag.

## 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-dom-event.js"></script> <script src="calendar.js"></script> <script src="history.js"></script>

## 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(); });

## Write the code that initializes your application

var calendar; function initCalendar (startDate) { // Instantiate the calendar control... calendar = new YAHOO.widget.Calendar("calendar", "calendarContainer", startDate); calendar.beforeRenderEvent.subscribe(handleCalendarBeforeRender, calendar, true); calendar.render(); }

## Use the Browser History Manager `onReady` method

Use the Browser History Manager `onReady` method 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)

YAHOO.util.History.onReady(function () { var currentState; // This is the tricky part... The onLoad event is fired when the user // comes back to the page using the back button. In this case, the // actual month that needs to be loaded corresponds to the last month // visited before leaving the page, and not the initial month. This can // be retrieved using getCurrentState: currentState = YAHOO.util.History.getCurrentState("calendar"); initCalendar({ pagedate: currentState.replace("_", "/") }); });

## 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, newState, currentState; calDate = calendar.cfg.getProperty("pageDate"); newState = (calDate.getMonth() + 1) + "_" + calDate.getFullYear(); try { currentState = YAHOO.util.History.getCurrentState("calendar"); // 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... if (newState !== currentState) { YAHOO.util.History.navigate("calendar", newState); } } catch (e) { calendar.cfg.setProperty("pagedate", newState.replace("_", "/")); calendar.render(); } }

## Initialize the Browser History Manager

Simply call `YAHOO.util.History.initialize`, passing in the id of the input field and IFrame we inserted in our static markup:

// Initialize the browser history management library. try { YAHOO.util.History.initialize("yui-history-field", "yui-history-iframe"); } catch (e) { // The only exception that gets thrown here is when the browser is // not supported (Opera, or not A-grade) Degrade gracefully. initCalendar({ pagedate: initialCalendarState.replace("_", "/") }); }

## Configuration for This Example

You can load the necessary JavaScript and CSS for this example from Yahoo's servers. Click here to load the YUI Dependency Configurator with all of this example's dependencies preconfigured.

### Browser History Manager Examples:

### More Browser History Manager Resources:

- User's Guide (external)
- API Documentation
- Cheat Sheet PDF (external)

Copyright © 2010 Yahoo! Inc. All rights reserved.

Privacy Policy - Terms of Service - Copyright Policy - Job Openings