views:

379

answers:

4

JQuery UI tabs are implemented by named anchors in an unordered list. When you hover over one of the tabs you can see this in the link shown at the foot of the browser:

http://mysite/product/3/#orders

Above would be the "orders" tab for example. JQuery obviously intercepts the click to this anchor and opens the tab instead.

However if I bookmark the link above or link to it from elsewhere in the site the page does not open on the specific tab.

In the tab initialisation block I was considering putting in some code that looks for a named anchor in the URL and, if it finds one, does an index lookup of the tabs and calls the select on it. This would mean it will still work with JS switched off.

But is there an easier/nicer/better way?

+1  A: 

In versions of jQuery UI prior to 1.8 (not inclusive) that's pretty much the way you would have to do it. The tab extension (AFAIK) doesn't know to switch based on which anchor it is initialized with (when the page loads) so you have to do that bit manually (in the ready event, of course).

As another answer indicates, the new version of the tabs in jQuery UI 1.8 supports bookmarking out of the box.

casperOne
thanks, just wanted to be sure I wasn't missing anything obvious. Must be a common requirement
Chris Simpson
FWIW the demo they have on the jQuery UI site seems to initialize properly on anchor.
R0MANARMY
@R0MANARMY - I suggest you add an answer saying "this works in the latest version". I could mark you up then
Chris Simpson
+2  A: 

Found this example here:

if(document.location.hash!='') {
    //get the index from URL hash
    tabSelect = document.location.hash.substr(1,document.location.hash.length);
    $("#my-tabs").tabs('select',tabSelect-1);
}
hunter
perfect, I've wrapped this up in a helper function and called it from the load event of the tabs control, thanks
Chris Simpson
+1  A: 

The latest version of jQuery UI supports this functionality. See example here:

  1. Second tab active by default
  2. Third tab active by default
R0MANARMY
thanks, gone with the stable release for the time being but I'll upgrade when I can
Chris Simpson
BTW jQueryUI 1.8 is the current stable release (see section *What about jQuery UI 1.7?*). http://blog.jqueryui.com/2010/03/jquery-ui-18/
R0MANARMY
strange, they haven't updated the front page yet: Latest (1.8: jQuery 1.4+) Stable (1.7.2: jQuery 1.3+) - I'm using jquery 1.3 also so I would be upgrading both at the same time.
Chris Simpson
A: 

I use the Session plugin to accomplish this in a custom tab class

Jason