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.
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.
In our example, the calendar widget relies on the following markup:
1 | <div id="calendarContainer"></div> |
view plain | print | ? |
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.
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 | ? |
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)
Use the YAHOO.util.History.getBookmarkedState
method and default to the month corresponding to today's date:
1 | var today = new Date(); |
2 | var defaultCalendarState = (today.getMonth() + 1) + "_" + today.getFullYear(); |
3 | var bookmarkedCalendarState = YAHOO.util.History.getBookmarkedState("calendar"); |
4 | var initialCalendarState = bookmarkedCalendarState || defaultCalendarState; |
view plain | print | ? |
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:
1 | YAHOO.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 | ? |
1 | var calendar; |
2 | |
3 | function 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 | ? |
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)
1 | YAHOO.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 | ? |
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):
1 | function 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 | ? |
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. |
2 | try { |
3 | YAHOO.util.History.initialize("yui-history-field", "yui-history-iframe"); |
4 | } catch (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 | ? |
All YUI 2.x users should review the YUI 2.8.2 security bulletin, which discusses a vulnerability present in YUI 2.4.0-2.8.1.
Copyright © 2013 Yahoo! Inc. All rights reserved.