YUI recommends YUI3.

YUI 2 has been deprecated since 2011. This site acts as an archive for files and documentation.

This documentation is no longer maintained.

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.

Basic markup

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

1<div id="calendarContainer"></div> 
view plain | print | ?

Add the markup required by the Browser History Manager

1<iframe id="yui-history-iframe" src="assets/blank.html"></iframe> 
2<input id="yui-history-field" type="hidden"
view plain | print | ?

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:

1<link type="text/css" rel="stylesheet" href="calendar.css"/> 
2<script src="yahoo-dom-event.js"></script> 
3<script src="calendar.js"></script> 
4<script src="history.js"></script> 
view plain | print | ?

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:

1var today = new Date(); 
2var defaultCalendarState = (today.getMonth() + 1) + "_" + today.getFullYear(); 
3var bookmarkedCalendarState = YAHOO.util.History.getBookmarkedState("calendar"); 
4var initialCalendarState = bookmarkedCalendarState || defaultCalendarState; 
view plain | print | ?

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:

1YAHOO.util.History.register("calendar", initialCalendarState, function (state) { 
2    // Show the right month according to the "state" parameter: 
3    calendar.cfg.setProperty("pagedate", state.replace("_""/")); 
4    calendar.render(); 
5}); 
view plain | print | ?

Write the code that initializes your application

1var calendar; 
2 
3function initCalendar (startDate) { 
4    // Instantiate the calendar control... 
5    calendar = new YAHOO.widget.Calendar("calendar""calendarContainer", startDate); 
6    calendar.beforeRenderEvent.subscribe(handleCalendarBeforeRender, calendar, true); 
7    calendar.render(); 
8
view plain | print | ?

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)

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

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):

1function handleCalendarBeforeRender () { 
2    var calDate, newState, currentState; 
3 
4    calDate = calendar.cfg.getProperty("pageDate"); 
5    newState = (calDate.getMonth() + 1) + "_" + calDate.getFullYear(); 
6 
7    try { 
8        currentState = YAHOO.util.History.getCurrentState("calendar"); 
9        // The following test is crucial. Otherwise, we end up circling forever. 
10        // Indeed, YAHOO.util.History.navigate will call the module onStateChange 
11        // callback, which will call createCalendar, which will call calendar.render(), 
12        // which will end up calling handleCalendarBeforeRender, and it keeps going 
13        // from here... 
14        if (newState !== currentState) { 
15            YAHOO.util.History.navigate("calendar", newState); 
16        } 
17    } catch (e) { 
18        calendar.cfg.setProperty("pagedate", newState.replace("_""/")); 
19        calendar.render(); 
20    } 
21
view plain | print | ?

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:

1// Initialize the browser history management library. 
2try { 
3    YAHOO.util.History.initialize("yui-history-field""yui-history-iframe"); 
4catch (e) { 
5    // The only exception that gets thrown here is when the browser is 
6    // not supported (Opera, or not A-grade) Degrade gracefully. 
7    initCalendar({ pagedate: initialCalendarState.replace("_""/") }); 
8
view plain | print | ?

Copyright © 2013 Yahoo! Inc. All rights reserved.

Privacy Policy - Copyright Policy - Job Openings