This example demonstrates how to use the Browser History Manager to remember which tabs have been visited with the TabView widget and dynamically update it when the user presses the browser's back/forward buttons.
Tab One Content
In our example, the TabView widget relies on the following markup:
1 | <div id="demo" class="yui-navset yui-navset-top"> |
2 | <ul class="yui-nav"> |
3 | <li><a href="#tab1"><em>Tab One Label</em></a></li> |
4 | <li title="active" class="selected"><a href="#tab2"><em>Tab Two Label</em></a></li> |
5 | <li title="" class=""><a href="#tab3"><em>Tab Three Label</em></a></li> |
6 | </ul> |
7 | <div class="yui-content"> |
8 | <div style="display: none;" id="tab1"><p>Tab One Content</p></div> |
9 | <div style="display: block;" id="tab2"><p>Tab Two Content</p></div> |
10 | <div style="display: none;" id="tab3"><p>Tab Three Content</p></div> |
11 | </div> |
12 | </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, TabView Widget, and the Browser History Manager:
1 | <link rel="stylesheet" type="text/css" href="tabview.css"/> |
2 | <script src="yahoo-dom-event.js"></script> |
3 | <script src="element-beta.js"></script> |
4 | <script src="tabview.js"></script> |
5 | <script src="history.js"></script> |
view plain | print | ? |
In our simple example, we have only one module, represented by the TabView widget. We will refer to this module using the identifier
"tabview". The state of the TabView module will be represented using the string "tab"
followed by the selected tab
index (e.g. "tab2"
if the third tab is selected)
Use the YAHOO.util.History.getBookmarkedState
method and default to the first tab:
1 | var bookmarkedTabViewState = YAHOO.util.History.getBookmarkedState("tabview"); |
2 | var initialTabViewState = bookmarkedTabViewState || "tab0"; |
view plain | print | ? |
Use the YAHOO.util.History.register
method, passing in the TabView module identifier, the initial state of the TabView
module, and the callback function that will be called when the state of the TabView module has changed:
1 | YAHOO.util.History.register("tabview", initialTabViewState, function (state) { |
2 | // Select the tab according to the "state" parameter: |
3 | tabView.set("activeIndex", state.substr(3)); |
4 | }); |
view plain | print | ? |
1 | var tabView; |
2 | |
3 | function initTabView () { |
4 | // Instantiate the TabView control... |
5 | tabView = new YAHOO.widget.TabView("demo"); |
6 | tabView.addListener("activeTabChange", handleTabViewActiveTabChange); |
7 | } |
view plain | print | ? |
onReady
method
Use the Browser History Manager onReady
method to instantiate the TabView widget. Also, retrieve the current
state of the TabView module, and use that state to select the right tab (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 | |
4 | initTabView(); |
5 | |
6 | // This is the tricky part... The onLoad event is fired when the user |
7 | // comes back to the page using the back button. In this case, the |
8 | // actual tab that needs to be selected corresponds to the last tab |
9 | // selected before leaving the page, and not the initially selected tab. |
10 | // This can be retrieved using getCurrentState: |
11 | currentState = YAHOO.util.History.getCurrentState("tabview"); |
12 | tabView.set("activeIndex", currentState.substr(3)); |
13 | }); |
view plain | print | ? |
A new history entry must be added every time the user selects a tab. Use the TabView widget's activeTabChange
event handler (set to handleTabViewActiveTabChange
- see above):
1 | function handleTabViewActiveTabChange (e) { |
2 | var newState, currentState; |
3 | |
4 | newState = "tab" + this.getTabIndex(e.newValue); |
5 | |
6 | try { |
7 | currentState = YAHOO.util.History.getCurrentState("tabview"); |
8 | // The following test is crucial. Otherwise, we end up circling forever. |
9 | // Indeed, YAHOO.util.History.navigate will call the module onStateChange |
10 | // callback, which will call tabView.set, which will call this handler |
11 | // and it keeps going from here... |
12 | if (newState != currentState) { |
13 | YAHOO.util.History.navigate("tabview", newState); |
14 | } |
15 | } catch (e) { |
16 | tabView.set("activeIndex", newState.substr(3)); |
17 | } |
18 | } |
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 | initTabView(); |
8 | } |
view plain | print | ? |
Copyright © 2008 Yahoo! Inc. All rights reserved.
Privacy Policy - Terms of Service - Copyright Policy - Job Openings