YUI Browser History Manager - TabView (YUI Library)

# YUI Browser History Manager: TabView

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.

## Import the source files and dependencies

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

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

## Basic markup

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

<div id="demo" class="yui-navset"> <ul class="yui-nav"> <li class="selected"><a href="#lorem"><em>lorem</em></a></li> <li><a href="#ipsum"><em>ipsum</em></a></li> <li><a href="#dolor"><em>dolor</em></a></li> </ul> <div class="yui-content"> <div id="lorem"> <p> Lorem ipsum dolor sit amet, consectetuer adipiscing elit, sed diam nonummy nibh euismod tincidunt ut laoreet dolore magna aliquam erat. </p> </div> <div id="ipsum"> <ul> <li><a href="#">Lorem ipsum dolor sit amet.</a></li> <li><a href="#">Lorem ipsum dolor sit amet.</a></li> <li><a href="#">Lorem ipsum dolor sit amet.</a></li> <li><a href="#">Lorem ipsum dolor sit amet.</a></li> </ul> </div> <div id="dolor"> <form action="#"> <fieldset> <legend>Lorem Ipsum</legend> <label for="foo"><input id="foo" name="foo"></label> <input type="submit" value="submit"> </fieldset> </form> </div> </div> </div>

## Design your application

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)

## Retrieve the initial state of the TabView module

Use the `YAHOO.util.History.getBookmarkedState` method and default to the first tab:

var bookmarkedTabViewState = YAHOO.util.History.getBookmarkedState( "tabview" ); var initialTabViewState = bookmarkedTabViewState || "tab0";

## Register the TabView module

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:

YAHOO.util.History.register( "tabview", initialTabViewState, function( state ) { // Select the tab according to the "state" parameter: tabView.set( "activeIndex", state.substr(3) ); } );

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

Use the Browser History Manager `onLoad` event handler 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)

var tabView; YAHOO.util.History.onLoadEvent.subscribe( function() { // Instantiate the TabView widget tabView = new YAHOO.widget.TabView( "demo" ); tabView.addListener( "activeTabChange", handleTabViewActiveTabChange ); // Retrieve the current state of the TabView module and select the appropriate tab var currentTabViewState = YAHOO.util.History.getCurrentState( "tabview" ); tabView.set( "activeIndex", currentTabViewState.substr(3) ); } );

## Add history entries

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

function handleTabViewActiveTabChange( e ) { var currentTabViewState = YAHOO.util.History.getCurrentState( "tabview" ); var newTabViewState = "tab" + this.getTabIndex( e.newValue ); // 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 tabView.set, which will call this handler and it keeps // going from here... if ( newTabViewState != currentTabViewState ) YAHOO.util.History.navigate( "tabview", newTabViewState ); }

## 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 >