How do I make simple tabs / accordion remember tabs when back button is used?

We have basic response tabs for tuning an accordion:

// Based on: http://codepen.io/jacmaes/pen/miBKI

// initialise when dom is ready
$(document).ready(function() {

  // initialise if .accordion is on the page
  if ($('.accordion')[0]) {

    // desktop initialisation
    if ($(window).width() > 736) {
      $('.accordion-content:not(:first)').hide(); // hide all but first tab content on larger viewports
      $('.accordion-title:first-child').addClass('active'); // activate first tab

    } else { // mobile initialisation
      $('.accordion-content').hide(); // hide all content items on narrow viewports
    };


    // Click functions
    $('.accordion-title').on('click', function() {

      // desktop click
      if ($(window).width() > 736) {
        $('.accordion-content').hide(); // hide all content
        $(this).next().show().prev().addClass('active').siblings().removeClass('active');
        return false; // stop browser jumping when tabs clicked

        } else { // mobile click
          $(this).toggleClass('active').next().toggle();
          return false; // stop browser jumping when tabs clicked
      };
    });
  }
});

      

We also use Snipcart, which appends #!/cart

to the end of any url if the cart is activated. And when the cart is closed, it leaves #!/

at the url.

How are we going to make tabs with a browser button and play well with Snipcart?

+3


source to share





All Articles