jQuery UI: Remember Your Open Tab

The jQuery UI library is a brilliant collection of official jQuery UI widgets and effects, one of them being the extremely useful Tabs widget.

Tabs allows you to shorten a page by placing bits of a page’s content into their own little “tabs”, which can be access by clicking on the tab header. A brilliant space saving mechanism and one that has become quite commonplace in applications these days.

Sometimes it is nice to remember what tab you currently have open so that when you travel to some other part of a site and then return to your original page, you want it to remember which tab you had currently selected.

Thankfully achieving this with jQuery UI’s tabs implementation is particularly easy.

Say hello to good old browser cookies.

To implement this built in functionality, you will need to include over and above the required jquery-ui.js and jquery.js libraries, the jquery.cookie.js plugin at the head of your page. This simplifies interaction with browser cookies and also provides a path for the tabs widget to use in order to save its state.

With this down, enabling tab state caching is a simple as declaring the tabs with a valid “cookie” option declaration:

$("#tabs").tabs({
cookie: {
name: 'tabs',
expires: 1
}
});

And this it. Simple as pie really. Note the ‘name’ option passed to the cookie object. This is important as it allows you to have different cookies for your different tabs instantiations on the site.

Useful, eh? ;)

You might also enjoy:

About Craig Lotter

Craig Lotter is an established web developer and application programmer, with strong creative urges (which keep bursting out at the most inopportune moments) and a seemingly insatiable need to love all things animated. Living in the beautiful coastal town of Gordon's Bay in South Africa, he games, develops, takes in animated fare, trains under the Funakoshi karate style and for the most part, simply enjoys life with his amazing wife and daughter. Oh, and he draws ever now and then too.
This entry was posted in Technology & Code, Tutorials and tagged , , , , , . Bookmark the permalink.
    blog comments powered by Disqus