aidanlister / jquery-stickytabs

Provides persistant state (back and forward button support) for Bootstrap tabs
http://aidanlister.com/2014/03/persisting-the-tab-state-in-bootstrap/
MIT License
60 stars 33 forks source link

Added a fallback option if no hash is given, but hash has changed. #1

Closed iNaD closed 9 years ago

iNaD commented 9 years ago

Hi,

I needed a fallback if the user hits the back button and the hash changes to nothing. So now it's simply possible to call stickyTabs like this:

$('.nav-tabs-sticky').stickyTabs({
    fallbackHash: '#home'
});

I also increased the version number and added a simple test.

aidanlister commented 9 years ago

Shouldn't your fallback hash be whatever tab is selected via "li.active > a"? That keeps it consistent with your CSS. Can you explain what situations you wouldn't want that behaviour? Thanks for your PR :)

iNaD commented 9 years ago

Ok I'll try to explain step by step.

  1. You open up the page with your tabs. URL is Like http://mypage.tld
  2. You switch to tab-2. URL is now http://mypage.tld#tab-2
  3. You switch to tab-3. URL is now http://mypage.tld#tab-3
  4. You navigate back with your browser. URL should be http://mypage.tld#tab-2 again and tab-2 selected
  5. Now if you navigate back - again - the URL should be http://mypage.tld and the default tab should be selected, but it isn't, because the current li.active > a is tab-2.

The reason for that is, tab-2 has the .active class and the default tab (e.g. tab-1) hasn't. So you have some options to decide which one is the default tab:

  1. Remember the selected tab when the class is initialized. In that moment always the default tab should be selected and change afterwards by this plugin
  2. Use the last one with active class (current behaviour)
  3. My solution with an option, which is nice, but not as good as 1

When I did this PR I forgot that we could remember the initial selected tab just on initialization. Maybe I'll find the time to also add this feature.

I hope you understand the issue, what the current behaviour is and what the real behaviour should be.

iNaD commented 9 years ago

Hi,

I updated the script. Now the default tab is remembered on initialization and used if no hash is given while you are going back in the history.

aidanlister commented 9 years ago

Can you see if this gist would work for you? https://gist.github.com/aidanlister/1d36815809977b21e0f0

aidanlister commented 9 years ago

Fixed now

iNaD commented 9 years ago

Hey Aidan,

good job. Had to use your little helper again and it works fine. Sorry that I missed to answer.