This example demonstrates how to use the Browser History Manager to "ajaxify" a simple navigation bar.
The small portion of PHP code is responsible for including the content specified by the "section" parameter in the URL. This technique avoids having to rewrite common parts of a web site such as header and footer.
This page is already fully functional. However, clicking on the links in the navigation bar will refresh the entire page, including portions that are common to all the sections. This is highly inefficient (especially for a large web site), and using AJAX will allow us to optimize this. The idea is to use client-side scripting to intercept the click event, cancel it, and use the YUI Connection Manager to asynchronously load the content of the section, which we can then write to the document using innerHTML. The only downside of this approach is that it breaks the back/forward buttons, and individual sections cannot be bookmarked anymore. The Browser History Manager will help us work around this issue.
In our example, we need the Connection Manager, Event Utility, DOM Utility, and the Browser History Manager:
Use the YUI Connection Manager's asyncRequest
to achieve this:
In our simple example, we have only one module, represented by the navigation bar. We will refer to this module using the identifier "navbar". The state of the navigation module will be represented using the name of the corresponding section ("home", "overview", "products", etc.)
Use the YAHOO.util.History.getBookmarkedState
method to find out
the initial state of a module according to the URL fragment identifier
(which is present if the user had previously bookmarked the application).
In our example, we also use the YAHOO.util.History.getQueryStringParameter
method to find out the initial state of a module according to the query string
(which is present if the user reached the page using a search engine, or if
the user did not have scripting enabled when previously bookmarking the page).
Finally, default to "home":
Use the YAHOO.util.History.register
method, passing in
the navigation module identifier, the initial state of the navigation
module, and the callback function that will be called when the
state of the navigation module has changed:
First of all, we want to change the behavior of the links in the navigation bar.
In order to do this, we simply enumerate them, and attach to each individual anchor
an onclick
handler. In the onclick
handler, we cancel
the event's default behavior and do some custom action.
We also need to display the default section if a section was requested via the URL fragment identifier, and that section is different from the one loaded using PHP:
onLoad
event
Simply call YAHOO.util.History.initialize
from a script
block located right after the opening body tag:
In our example, we wrapped the call to YAHOO.util.History.initialize
by a try...catch
block. Indeed, this method may throw an exception
if the browser is not an A-grade browser. You may want to catch the exception
and do something, or test which browser you're on before calling this method.
Copyright © 2007 Yahoo! Inc. All rights reserved.
Privacy Policy - Terms of Service - Copyright Policy - Job Openings