changing location.hash with jquery ui tabs
Asked Answered
P

18

47

I've been trying to find a way to change the window.location.hash to the currently selected tab in Jquery UI Tabs.

I've tried:

$("#tabs > ul").tabs();
$("#tabs > ul").bind("tabsshow", function(event, ui) { 
window.location.hash = ui.tab;
})

This results in changing the hash to #undefined when the tab is changed.

I've also tried:

$("#tabs > ul").tabs({ 
select: function(event, ui) { 
window.location.hash = ui.tab }
});

But this doesn't seem to be triggered at all.

Any help would be appreciated. Thanks.

Edit: It looks like part of my initial problem had something to do with js somewhere else interfering with this. Both the accepted answer and the other suggested answer slightly modified do work. Thanks all.

Potato answered 20/2, 2009 at 16:35 Comment(4)
are you trying to open a link in the tab where the link clicked from ?Anadem
No, the links I'm opening are part of the current page itself, no ajax/etc.Potato
this is a pretty awesome demo of this technique: http://jqueryfordesigners.com/jquery-tabs/Piteous
Is there a workaround that prevents the page from jumping down to that hash anchor in the page when a tab is clicked?Kayseri
R
49

In your event handler function ui.tab is an anchor element. You can retrieve its hash value like this:

$("#tabs > ul").tabs();
$("#tabs > ul").bind("tabsshow", function(event, ui) { 
    window.location.hash = ui.tab.hash;
})

Does this work for you?

Ryley answered 20/2, 2009 at 17:16 Comment(4)
Unfortunately no, I receive many ui.tab is undefined errors upon page load or tab change.Potato
I've been trying with the demo that appears on the doc page and works fine with FF 3 and IE 7. Are you triggering the events manually?Ryley
I tried as you suggested with the demo page and your solution did end up working, thanks for the solution, I'll have to figure out what else in my code was stopping it from working. ThanksPotato
If you are testing on Firefox, install the Firebug extension and put a console.trace() to track where the problems come from.Ryley
P
25
$('#tabs').tabs({
    select: function(event, ui) {
        window.location.hash = ui.tab.hash;
    }
});
Paleozoology answered 19/10, 2010 at 13:46 Comment(0)
C
14

While some of the above solutions work, they cause the page to jump around in some cases, as the window.location.hash API is designed to bring you to a specific part of the page.

This neat solution proposed here, solves that issue

  $("#tabs").bind("tabsshow", function(event, ui) { 
    history.pushState(null, null, ui.tab.hash);
  });
Cruickshank answered 5/10, 2012 at 17:44 Comment(1)
This should probably be marked as the correct answer.Melgar
O
8

This worked for me, jQuery UI 1.10:

$('#elexumaps_events_tabs').tabs({
    activate: function(event, ui) { 
       window.location.hash=ui.newPanel.selector; 
    }
});

See also: http://api.jqueryui.com/tabs/#event-activate

Oblong answered 28/2, 2013 at 12:4 Comment(1)
this works well for me, however, I'm using ajax to load my tabs, so the hash is always "ui-tabs-1", "ui-tabs-2", etc... This means when I re-order, add, or remove tabs the bookmarks break. Do you know a way to do this where I can use custom hash names for the tabs?Embargo
H
7

my simple solution without jumping:

    $("#tabs").tabs({
        activate: function (event, ui) {
            var scrollPos = $(window).scrollTop();
            window.location.hash = ui.newPanel.selector;
            $(window).scrollTop(scrollPos);
        }
    });
Hexangular answered 17/2, 2014 at 15:42 Comment(0)
H
4

Would this be what you're going for?

$("#tabs > ul").tabs({ 
   select: function(event, ui) 
   { 
      window.location = "#" + ui.tab;
   }
});
Holophrastic answered 20/2, 2009 at 16:52 Comment(3)
I tried this without any luck. I even tried putting an alert prior to the window.location setting and it doesn't appear when changing tabs. The only way I could get it to activate was by binding tabsshow but that ends up with an undefined ui.tabPotato
When I tried using the demo code this solution slightly modified did work. The only change I would suggest is making it: window.location.hash = ui.tab.hash;Potato
This worked as the best solution for me. Other suggestions resulted in the page jumping to the tab selected when the location hash was updated. Seconding "window.location.hash = ui.tab.hash;" in place of what is provided in the example, tho.Consignment
T
4

Many of the above answers don't work with the current version of jQuery UI Tabs. Here's what I'm currently using:

var tabsUi = $('#tabs');
tabsUi.tabs();

// Add hash to URL when tabs are clicked
tabsUi.find('> ul a').click(function() {
    history.pushState(null, null, $(this).attr('href'));
});

// Switch to correct tab when URL changes (back/forward browser buttons)
$(window).bind('hashchange', function() {
    if (location.hash !== '') {
        var tabNum = $('a[href=' + location.hash + ']').parent().index();
        tabsUi.tabs('option', 'active', tabNum);
    } else {
        tabsUi.tabs('option', 'active', 0);
    }
});
Turd answered 19/6, 2013 at 12:35 Comment(1)
Perfect solution, the only one that worked for me :)Either
D
3
$('#tabs').tabs({
    select: function(event, ui){
      window.location = ui.tab.href;
    }
});
Dinger answered 9/5, 2009 at 14:43 Comment(0)
F
3

My way for jQuery UI 1.10.3

$("#tabs").tabs({
   beforeActivate: function(event, ui) {
        var hash = ui.newTab.children("li a").attr("href");
        window.location.hash = hash;
   }
});
Fosque answered 23/9, 2013 at 11:39 Comment(0)
A
1

I'm using a tab plugin you can find it at github: https://github.com/jquerytools/jquerytools.github.com

Alcock answered 27/9, 2010 at 9:18 Comment(0)
L
1

This worked for me using Jquery 1.8

$('#tabs').tabs({
    activate: function(event, ui) {
       window.location.hash = ui.newPanel.attr('id');
    }
});
Loculus answered 31/10, 2013 at 20:46 Comment(0)
U
1

After looking through some other questions and the jQueryUI API docs I found that this ended up working for me.

$(document).ready(function() {
    $("#tabs").tabs({
        activate: function( event, ui ) {
            location.hash = ui.newTab.children(".ui-tabs-anchor").attr("href").substr(1);
        }
    });
});
Usher answered 12/1, 2015 at 19:54 Comment(0)
T
0

It seems like ui.tab itself doesn't return a valid string. (instead it returns undefined, so you'd say it doesn't return anything at all.)

Try looking around in the dev version of ui.jquery.js whether there are any returns there, maybe you have to call a child of ui.tab ;-)

Tenstrike answered 20/5, 2009 at 15:7 Comment(1)
@Tenstrike you need ui.tab.index ;)Replevy
A
0

This code works ok for me:

$('#tabs').tabs({
    select: function(event, ui) { 
        window.location = $(ui.tab).attr('href');
    }
});
Antares answered 5/2, 2014 at 12:43 Comment(0)
P
0

This code works for me :

$("#tabs").tabs({
    activate: function(event, ui) { 
       window.location.hash=ui.newPanel.selector; 
    }
});
Pertain answered 23/3, 2016 at 9:18 Comment(0)
A
0

The following code is worked for me..

$("#tabs").tabs({
   activate : function(event, ui) {
     window.location.hash = ui.newPanel[0].id;
  }
});
Almeta answered 7/2, 2017 at 4:45 Comment(0)
A
0

My working solution.

jQuery v3.5.1, jQuery UI v1.12.1

$(".detail-groups").tabs({ activate: function(event, ui) {
                            window.location.hash = ui.newPanel[0].id;
                         }
                    });
Athey answered 3/6, 2020 at 12:17 Comment(0)
O
-1

This piece of code worked for me:

window.location.hash="";
Orland answered 11/7, 2014 at 11:14 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.