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.
Client-side changes to a web page's content and structure are not recorded by the browser's history engine. As a consequence, the navigation buttons (back/forward) we've learned to use as we traverse the World Wide Web of documents don't usually serve us well when we begin exploring the World Wide Web of applications. Bookmarking, too, is problematic in web applications, as the application can change state hundreds of times through the course of a session without any change to the original document's URL. These are significant problems in the current paradigm of web application development.
The YUI Browser History Manager is a utility designed to facilitate the creation of web applications in which the navigation buttons are fully functional and in which broad aspects of an application's state — what panels are open, what tabs are active, etc. — can be bookmarked.
Note: Opera is not supported by the Browser History Manager at this time. Please refer to the Known Limitations section for more information.
To use the Browser History Manager, include the following source files in your web page:
<!-- Dependencies --> <script src="http://yui.yahooapis.com/2.9.0/build/yahoo/yahoo-min.js"></script> <script src="http://yui.yahooapis.com/2.9.0/build/event/event-min.js"></script> <!-- Browser History Manager source file --> <script src="http://yui.yahooapis.com/2.9.0/build/history/history-min.js"></script>
<!-- Dependencies --> <script src="http://yui.yahooapis.com/2.9.0/build/yahoo/yahoo-min.js"></script> <script src="http://yui.yahooapis.com/2.9.0/build/event/event-min.js"></script> <!-- Browser History Manager source file --> <script src="http://yui.yahooapis.com/2.9.0/build/history/history-min.js"></script>
Instead of copying and pasting the filepaths above, try letting the YUI dependency Configurator determine the optimal file list for your desired components; the Configurator uses YUI Loader to write out the full HTML for including the precise files you need for your implementation.
Note: If you wish to include this component via the YUI Loader, its module name is history
. (Click here for the full list of module names for YUI Loader.)
Where these files come from: The files included using the text above will be served from Yahoo! servers. JavaScript files are minified, meaning that comments and white space have been removed to make them more efficient to download. To use the full, commented versions or the -debug
versions of YUI JavaScript files, please download the library distribution and host the files on your own server.
Order matters: As is the case generally with JavaScript and CSS, order matters; these files should be included in the order specified above. If you include files in the wrong order, errors may result.
This section describes the most common use of the Browser History Manager. It contains the following subsections:
onReady
methodThe Browser History Manager requires the following in-page markup:
<iframe id="yui-history-iframe" src="path-to-existing-asset"></iframe> <input id="yui-history-field" type="hidden">
<iframe id="yui-history-iframe" src="path-to-existing-asset"></iframe> <input id="yui-history-field" type="hidden">
Notes:
src
attribute to make sure of that)<body>
tag.Don't forget to hide the IFrame using the following CSS code:
#yui-history-iframe { position: absolute; top: 0; left: 0; width: 1px; height: 1px; visibility: hidden; }
#yui-history-iframe { position: absolute; top: 0; left: 0; width: 1px; height: 1px; visibility: hidden; }
A user interface module is simply a visible item on your page with which the user can interact. As such, modules are not immutable and their state is subject to change over time. If you wish to record the history of states visited by a specific module during a single browser session, to allow the user to navigate through that history using the browser's back/forward button, and to bookmark the page with your module in a specific state, you must first register it with the Browser History Manager.
In order to register a module, simply call the YAHOO.util.History.register
method, passing in
the module identifier (a non-empty string that uniquely identifies your module), the initial state of
your module (i.e. the state of your module corresponding to the earliest history entry within
the current browser session) as a string, and a callback function that will be invoked by the Browser
History Manager every time your module changes state (so you can update the UI of your module
accordingly).
Getting the initial state of the module is the trickiest part of this process. Indeed, the user might have
previously visited your page and bookmarked it with your module in a specific state. If the
user later on goes to your page using the same bookmark, you will likely wish to restore your module in
the same state it was in when the page was bookmarked. To retrieve this "bookmarked state", simply
call YAHOO.util.History.getBookmarkedState
, passing in your module identifier:
var myModuleBookmarkedState = YAHOO.util.History.getBookmarkedState("myModule");
var myModuleBookmarkedState = YAHOO.util.History.getBookmarkedState("myModule");
Of course, you also need a "default state" for your module in case the user accessed
the page without using a bookmark. Having determined whether there is a bookmarked state in the code example above (getBookmarkedState
will have returned null
if there was no bookmarked state), the initial state of your module can be assigned as follows:
// If there is no bookmarked state, assign the default state: var myModuleInitialState = myModuleBookmarkedState || "myModuleDefaultState";
// If there is no bookmarked state, assign the default state: var myModuleInitialState = myModuleBookmarkedState || "myModuleDefaultState";
The next step in configuring your module to use the YUI Browser History Manager is to define your module's onStateChange
callback. This callback
will be called every time the Browser History Manager has detected that the state of your
module has changed (which may happen when the user triggers the browser's back/forward button,
for example).
function myModuleStateChangeHandler (state) { // Update the UI of your module according to the "state" parameter //... }
function myModuleStateChangeHandler (state) { // Update the UI of your module according to the "state" parameter //... }
Finally, complete your module registration by calling YAHOO.util.History.register
:
YAHOO.util.History.register("myModule", myModuleInitialState, myModuleStateChangeHandler);
YAHOO.util.History.register("myModule", myModuleInitialState, myModuleStateChangeHandler);
Note: You may register as many modules as you want, but you must register at least one module before initializing the Browser History Manager. Modules cannot be registered once the Browser History Manager has been initialized.
onReady
method
The Browser History Manager will initialize itself asynchronously and some of its methods
cannot be called until it is fully initialized. You may want to know when this event occurs.
In order to do that, you could subscribe to the Browser History Manager's onLoad
event. However, using this approach, and if the subscription happens after the Browser History
Manager has been initialized, the event never fires. Therefore, it is recommended to use the
onReady
method instead.
One common task you'll find yourself executing in your Browser History Manager onReady
callback is retrieving the current state of your module and updating its UI accordingly. Why would
the current state of your module be different from its initial state? Remember that the "initial"
state of your module corresponds to the earliest history entry. The Browser History
Manager onLoad
event will be fired every time the user visits the page. If the user
comes back to the page using the browser's back button, the state of your module will correspond
to the latest history entry — that is the last state of your module before the user left the
page. You can retrieve the current state of your module using YAHOO.util.History.getCurrentState
,
passing in your module identifier:
YAHOO.util.History.onReady(function () { //... var myModuleCurrentState = YAHOO.util.History.getCurrentState("myModule"); // Update the UI of your module according to "myModuleCurrentState" //... });
YAHOO.util.History.onReady(function () { //... var myModuleCurrentState = YAHOO.util.History.getCurrentState("myModule"); // Update the UI of your module according to "myModuleCurrentState" //... });
The last step is to initialize the Browser History Manager, passing in a reference — or the id — of the required HTML elements previously created:
YAHOO.util.History.initialize("yui-history-field", "yui-history-iframe");
YAHOO.util.History.initialize("yui-history-field", "yui-history-iframe");
Note: An exception may be thrown when initializing the Browser History Manager
if the browser is not officially supported. Therefore, you may want to wrap this call within a
try...catch
block.
Call YAHOO.util.History.navigate
to store a new state in the browser history,
passing in the identifier of the module which state has changed, and a string representing
the new state of the specified module:
YAHOO.util.History.navigate("myModule", "myModuleNewState");
YAHOO.util.History.navigate("myModule", "myModuleNewState");
The corresponding module onStateChange
callback, specified at the time of the
module registration, will then be called asynchronously (up to 50 msec later), giving you
the opportunity to update the UI of your module. Note: The same callback
will be called when the state of your module has changed after the user triggered the
browser's back/forward button. There is no way to discriminate between these two situations.
The Browser History Manager only provides a way of storing/retrieving the history of states visited by an application. It is up to the implementer to name and handle each state appropriately.
location.hash
property when using the back/forward buttons. We
expect this regression to be corrected in a future version of the Opera web browser.
The YUI Library and related topics are discussed on the on the YUILibrary.com forums.
Also be sure to check out YUIBlog for updates and articles about the YUI Library written by the library's developers.
The YUI Library's public bug tracking and feature request repositories are located on the YUILibrary.com site. Before filing new feature requests or bug reports, please review our reporting guidelines.
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.