0

I found a useful snippet here to make Quicktabs remember which tab was open if the user leaves the page and then comes back again. My problem is that I want to remove the history (the cookie) after the user has payed a visit to a certain page. In that case I want the default tab to be opened when the user comes back again. In the Quicktabs settings I have set the wanted default tab.

I have a script.js in my theme with this snippet:

  Drupal.behaviors.quicktabsHistory = {
    attach: function(context, settings) {
        var id = $(this).attr('id');
        var tab = $.cookie(id);     

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

        $('.view-taxonomy-term', context).each(function() {
            if (tab != '') {
              $.removeCookie('id');
            }
        });
    }
};

Everything works fine, except for the part where I want to loose the history. How can I remove the cookie if the user visits the page with the wrapper class ".views-taxonomy-term"?

EDITED: I tried changing the removeCookie row to insteaed force the tab I want to be the default one:

    $('.view-taxonomy-term', context).each(function() {
        if (tab != '') {
          tab = 'quicktabs-tab-instrument_flikar-0';
        }
    });

But that did not do the trick. I have checked though with a breaking point in the script that the tab variable actually is set to the new value, but the previous tab is still activated after return to the page with the tabs. There is something about Drupal.behaviors.quicktabsHistory that I don't understand, obviuosly. :-(

  • Surely that should be $.removeCookie(id);? – Clive Jan 14 '16 at 16:19
  • Please don't put tags in title field, unless they are natural part of sentence. – Mołot Jan 14 '16 at 17:33
  • Sorry about "drupal 7" in the title. Didn't think that through... – TBJ Jan 15 '16 at 8:38
  • The removeCookie('id') surely works but I must have done some thing wrong, because the previous tab i still open when I come back from a visit on the view-taxonomy-term page. Could use some ideas... – TBJ Jan 15 '16 at 8:40
0

Finally solved it. In case anyone is stuck with the same problem, this code now works for me. I learned that it's important to always set the path, to be able to retrieve the same cookie on different pages. I never got $.removeCookie() to work, so instead I used a default value. Also it did not work to use true, false and undefined as values, but it worked to use them as text inside quotes, 'true', 'false' and 'undefined'. Slowly getting there :-).

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

        var default_tab = 'quicktabs-tab-instrument_flikar-0';
        if($.cookie('id', {path:'/'}) == 'undefined'){
            var id = $(this).attr('id');
        }else{
            var id = $.cookie('id', {path:'/'});
        }
        var tab = id;
        var flagga = $.cookie('flagga', {path:'/'});

        if($("body").is(".page-taxonomy")) {
            $.cookie('flagga', true, {path:'/'});
            $.cookie('id', default_tab, {path:'/'});
        }           

        $('.quicktabs-wrapper', context).each(function() {
            if (tab != '' && flagga === 'false') {
              $(this).find('ul.quicktabs-tabs a#' + tab).click();
            }
            else {
              $.cookie('flagga', false, {path:'/'});                    
              $(this).find('ul.quicktabs-tabs a#' + default_tab).click();
            }

            $(this).find('ul.quicktabs-tabs a').click(function() {
                $.cookie('id', $(this).attr('id'), {path:'/'});
            });
        });
    };
| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.