The code provides a js only solution to tab and accordion history, i.e. whichever quicktabs tab or accordion clicked will be remembered via cookie so when you refresh or navigate to other pages, the correct tab and accordion will be 'loaded' (virtually clicked).

  Drupal.behaviors.quicktabsHistory = {
    attach: function(context, settings) {
      // enable accordion memory
      $('.quick-accordion', context).each(function() {
        var id = $(this).attr('id');
        var accordion = $.cookie(id);

        if (accordion != '') {
          $(this).find('h3 a[href="' + accordion + '"]').click();
        }
        $(this).find('h3 a').click(function() {
          $.cookie(id, $(this).attr('href'));
        });
      });

      // enable tab memory
      $('.quicktabs-wrapper', context).each(function() {
        var id = $(this).attr('id');
        var tab = $.cookie(id);

        if (tab != '') {
          $(this).find('ul.quicktabs-tabs a#' + tab).click();
        }
        $(this).find('ul.quicktabs-tabs a').click(function() {
          $.cookie(id, $(this).attr('id'));
        });
      });
    }
  };

If you do not have script.js or an empty one, wrap the above code with

(function ($) {
  // the code above
})(jQuery);

Inspiration from similar D6 thread #354199: Remember last clicked tab

Support from Acquia helps fund testing for Drupal Acquia logo

Comments

ckng’s picture

Status: Active » Needs review
kmare’s picture

Hello,
this may sound silly, but where exactly should I put this script?
Thank you in advance

ckng’s picture

Of course I would hope this will get into the module. Otherwise, just include it in your theme.

PadenLee’s picture

#2 + Bump

ckng’s picture

To include as part of your theme
- add a script.js file inside your theme folder, if not already have one
- include the script.js into your theme .info file, if not already have one
- add the code into existing script.js or the new script .js you just created
- clear theme cache, or visit admin/appearance

CinemaSaville’s picture

I followed your steps, but it's not working.

jdflorez’s picture

Worked for me. Added it in my theme.
Thanks!

gynekolog’s picture

Hi, i have the script.js in my theme, i see that drupal is loading script (in source code) but it doesn't work at all. If I reload page, quicktabs is still at default position. I was trayed dev and also stable version.

ckng’s picture

@CinemaSaville, @gynekolog
Make sure there are no other javascript error. That's the only thing I could think of which prevent it from working or you've alter the quicktab output.

gynekolog’s picture

FileSize
142.8 KB

@ckng

I'm getting this error -> Quicktabs history error

ckng’s picture

@gynekolog
I've added additional note on the first post, you are missing the jquery wrapper

gynekolog’s picture

It works now, thank you!

gynekolog’s picture

FileSize
17.51 KB

Next problem.. history is working only if click on main items in menu and not for child ->

Quicktabs history error

edit: excuse my english and my error in word "history" on picture.

mathieso’s picture

Thnx, ckng. You da man!

One problem. I was getting a JS error that $.cookie was not defined. Added the cookie jQuery plugin from:

https://github.com/carhartl/jquery-cookie/blob/master/jquery.cookie.js

Now it works.

Thnx again,
Kieran

Sinan Erdem’s picture

I tested the code in the issue with the plugin mentioned on comment #14 and it works half the time for me. Sometimes it remembers the correct tab, sometimes it doesnt. Please see it working here: www.profnews.nl (TOP 5 ACUTELE ARTIKELEN block on the right sidebar)

Sinan Erdem’s picture

I had to first remove cookie after clicking a tab to make it work consistently. Also I added the parameter "path" to make sure the cookie works in all the pages of the website.

Here is the revised JS code that is working for me (I only tested for tabs, not accordions):

(function ($) {
  Drupal.behaviors.quicktabsHistory = {
    attach: function(context, settings) {

      // enable accordion memory
      $('.quick-accordion', context).each(function() {
        var id = $(this).attr('id');
        var accordion = $.cookie(id);

        if (accordion != '') {
          $(this).find('h3 a[href="' + accordion + '"]').click();
        }
        $(this).find('h3 a').click(function() {
          $.removeCookie(id, {path: '/'});
          $.cookie(id, $(this).attr('href'), {path: '/'});
        });
      });


      // enable tab memory
      $('.quicktabs-wrapper', context).each(function() {
        var id = $(this).attr('id');
        var tab = $.cookie(id);

        if (tab != '') {
          $(this).find('ul.quicktabs-tabs a#' + tab).click();
        }
        $(this).find('ul.quicktabs-tabs a').click(function() {
          $.removeCookie(id, {path: '/'});
          $.cookie(id, $(this).attr('id'), {path: '/'});
        });
      });
    }
  };
})(jQuery); 
mmtt’s picture

I've tried both codes, script.js is loading with theme.info file. Have copied jquery.cookie.js in libraries directory but it does,nt work.
I got this error:
Only local images are allowed.

ckng’s picture

@mmtt, add the jquery.cookie.js to your theme info file, before the script.js, clear theme cache

No Sssweat’s picture

nvm I figured it out.

No Sssweat’s picture

Just wanted to point out that this Java Script does NOT work if you select the "Load all tabs on page view" option. http://s21.postimg.org/aj5yvej79/quicktabs.jpg

ultimateboy’s picture

Status: Needs review » Active

No patch. Setting back to active.

ultimateboy’s picture

Issue summary: View changes

added jquery wrapper for those without one

Elijah Lynn’s picture

Issue summary: View changes
Related issues: +#354199: Remember last clicked tab
Elijah Lynn’s picture

Title: Tab/accordion history » D7: Tab/accordion history

Prefixing title with D7 as to improve clarity as to which issue is for D6 vs D7.

Doing the same for #354199: Remember last clicked tab.

Elijah Lynn’s picture

Status: Active » Needs review
FileSize
3.61 KB

Attached is a patch that builds off of the above snippets but is heavily modified.

I also snuck in some coding standards improvements in case this patch gets in. This patch leaves out the accordian code because I just couldn't test it and I figure we could maybe get this in and then get the accordian stuff in with a follow up patch.

Per #20, this new patch appears to work with either the Ajax/non Ajax option. I also refactored it to use a unique cookie name in case there are multiple quicktabs groups on a page although I did not test that part.

Elijah Lynn’s picture

Here is an improved patch which sets the cookie to be site-wide and not just the current page. There is not expiration option set because by default it is a session cookie and I think it should stay that way.

markbannister’s picture

Used patch #25 on 7.x-3.6 and it seems to work.

Elijah Lynn’s picture

Status: Needs review » Reviewed & tested by the community

Based on #26 I am going to mark RTBC then.

Elijah Lynn’s picture

Status: Reviewed & tested by the community » Needs review

Argh, actually it was just tested by the community, not reviewed. Changing back to needs review.

Anonymous’s picture

#25 worked for me as well.
However, as a side note (which may or may not be related):
I use the tinynav.js module to change the quicktbabs tabs to a dropdown menu when the browser window is a smaller size. After implementing this patch, the dropdown items no longer work.
The dropdown items are basically linking to the tabs direct link.

Edit:Direct links do not seem to work either.

WorldFallz’s picture

Status: Needs review » Reviewed & tested by the community
FileSize
3.43 KB

No longer applies. Updated patch attached. Patch works as advertised, fixes coding standards, and, more importantly, enables quicktabs to work as as users expect (i get this complaint on every quicktab I deploy if I forget to add this patch, lol).

markbannister’s picture

This still does not work on https sites. I switched to https recently and this stopped working.
Tried latest patch #30

WorldFallz’s picture

Where in this thread does it say it doesn't work with https? I reread it and couldn't find any reference to https. And looking through the code, it's not obvious to me why it would matter or even know if the site is http/https.

markbannister’s picture

WorldFallz:
Well there you go. I can't find any reference to it either and I could have sworn I read that some time ago..... OK... cleared the cache a few more times and the latest patch (#30) is now working both on my dev and live sites.
So I either overwrote the patch at some point....
I should change my user name to CryingWolf.

WorldFallz’s picture

lol, no worries-- it happens to all of us. I just didn't want this patch diverted if it wasn't an actual issue.

RdeBoer’s picture

Some time has passed since the patch of #25, redone as #30. Thanks everyone for getting it this far!

On my Drupal 7.38 install I found that the code works when
1) user is logged in as admin and
2) there is only one set of Quicktabs on the page.

I have 3 sets of Quicktabs on the same page and I also wanted these to work for anonymous users.
Making it work for multiple sets on the page needed only $this.find inserted in the patch of #30, inside the quicktabsCookieClickHandler():

        $activeTabLink = $('.quicktabs-tabs li.active a', context);

becomes

        $activeTabLink = $this.find('.quicktabs-tabs li.active a', context);

I traced down the reason why it wasn't working for anonymous users to the fact that /misc/jquery.cookie.js only gets included when the system library known under machine name ‘jquery.cookie’ is loaded. This happens when the admin has the Toolbar enabled. But when jquery.cookie.js is NOT loaded, the call $.cookie(...) will result in a JS error, visible in your browser console.

To make the code work for anonymous users (for whom Toolbar is not enabled) I explicitly included this library in the first line of function quicktabs_load_multiple() in quicktabs.module

function quicktabs_load_multiple($names = array()) {
  drupal_add_library('system', 'jquery.cookie');
  // original code starts here...
  // ...
}

I tested this patch locally as well as on a secure (HTTPS) live site. It seems to work.

Suggest you clear your browser cookies before you start experimenting with the new patch.
The patch was created agains the current OFFICIAL version 7.x-3.6, not the dev branch.

Enjoy!
Rik

Tommy_001’s picture

# 16 works fine for me, except that I would like the default tab to open when the user opens another node.
Back to same node = remember and activate the selected tab.
Back to another node (with the same tabs) = use the default tab.
Any suggestions on how the code snippet could be adapted to do this?
I haven't got very far in my own attempts so far...

EDITED: it turns out that the code actually already does this... sorry about the noise.

WorldFallz’s picture

Keep coming back to this issue, lol. Patch in 35# still applies and does work on pages with multiple quicktabs as advertised, and takes care of having the library loaded in all cases. Would be really nice to get this in.

Hydra’s picture

Can confirm that #35 is still applying and working like expected, thx!

adrianavaz’s picture

Hi.

I have an online site and client just asked for this. I heard patches shouldn't be applied to production sites and I'm afraid I could break it.
Is there any other way I can solve this? Should I wait for a new release of the module with this feature?

Thank you.

joelpittet’s picture

@adrianavaz patches that you've tested are ok to apply to production sites if they fix a problem or adds a needed feature. If this solves the problem you should test it out locally and if it works, push it to production. Keep track somewhere that the patch was applied. I do so by including the patch in the directory it is applied.

NWOM’s picture

#35 worked great. Thank you!

Gold’s picture

+1 on the RTBC. The patch at #35 is working as advertised for me too.

Gold’s picture

Status: Reviewed & tested by the community » Needs work

Update...

This works okay with the Renderer set to "quicktabs". It's not working with the "ui_tabs" or "accordion" renderers.

markdc’s picture

Patch #35 works well, but it ignores direct links. A query string in the URL for a specific tab should be respected and should replace the cookied history page with the requested page.

delacosta456’s picture

hi many thanks for this.
however i was expecting this patch to also remember the tab on wich we were before refreshing the page..
On refresh page reload with tab set as default ..
May somebody can help extend this patch to that functionality which may be a king of enable/disable option in quicktabs settings

thanks

jmuzz’s picture

Title: D7: Tab/accordion history » D7: Tab history
Status: Needs work » Reviewed & tested by the community
Related issues: +#2897890: D7: Accordion history

As stated in #24 accordion was removed from the patch to try to make something focused that could hopefully be committed sooner than a patch which adds multiple features. I have created the separate issue for that.

delacosta456’s picture

Hi ok i am back. after multiple .

After applying the patch ..and flushing twice it work as expected .. i mean on page refresh... BUT BEHAVE STRANGLY WITH CACHING METHODS/MUDULE. Example below

  1. With Entity cache Module ONLY enabled for all views --> quicktabs always reset to FIRST AND MAIN tab(Main mean that in case sub-tab are present)

  2. With Views Content Cache ONLY enabled --> quicktabs tabs REMEMBERING(this patch) WORKS AS EXPECTED

  3. IF DIFFERENT CACHE MODULES/METHODS ARE USED () in QUICKTAB WITH MULTIPLE TABS AND SUBTABS (**Tab1**has view cache with Views Content Cache, **Tab2** and **Sub**Tab2** has view cache with default views embeded cache option) ONLY enabled --> AGAIN quicktabs always reset to FIRST AND MAIN tab(Main mean that in case sub-tab are present)

Am i missing somthing? is it related to caching methods ? or patch still needs more reviews?

Many thanks

Yannick Perret’s picture

Hi. We are using ui_tabs, so I tried to adapt #35 patch for that. Here is the corresponding patch, to apply to qt_ui_tabs.js (note: I guess the quicktabs.module's patch (adding cookie lib) should be present whatever).

At first tests it works for me (quicktabs including views, with ajax enabled).

Maybe as for accordions this may go into a separated issue?

Note: as functions are roughtly the same for quicktabs and ui_tabs (only classes name change) it should maybe be factorized.

Any feedback welcome.

Status: Reviewed & tested by the community » Needs work

The last submitted patch, 48: qt_ui_tabs.js_.patch, failed testing. View results

Yannick Perret’s picture

FileSize
1.59 KB

Bad patch format. New one inserted (ways to replace an attachment?).

Note: not sure but all this may interact with feature that let select active tab ('qt-TAB_NAME=NUMBER'), no?

Yannick Perret’s picture

Related issues: +#2979398: D7: ui_tab history
deepakkm’s picture

This worked for me , thanks for a perfect solution.

sarathkm’s picture

Adding similar issue in drupal 8

butterwise’s picture

Just when I thought I was done with Drupal 7... #35 worked for me, version 7.x-3.8. Now back to migrating that site to D9 :)