• Resolved johnwandler

    (@johnwandler)


    I’m currently using script from documentation. Everything is fine – url adds hash, it scrolls to that tab, cool. Problem is when url actually contains specific hash (let’s say “#specification”) when opened in new tab or after page refresh, that custom link will no longer scroll to that tab on click. It will scroll after page refresh etc., but not on click.

Viewing 1 replies (of 1 total)
  • Thread Starter johnwandler

    (@johnwandler)

    I think i’ve made it. At least i think so.

    const tabOpener = function(tab) {
      // Simulate a click on that tab.
      if (typeof tab === 'string') {
        const currentTab = jQuery('.' + tab + '_tab');
        currentTab.children('a').click();
      }
    
      // Scroll to that tab.
      jQuery('html, body').animate(
        {
          scrollTop: jQuery('#tab-' + tab).offset().top - $('.site-header').height()
        },
        300,
        function() {
          // Update the URL hash after scrolling is complete.
          window.location.hash = tab;
        }
      );
    };
    
    jQuery(document).ready(function($) {
      const { hash } = window.location;
      let tab;
    
      // If a # exists in the URL, let's see if it's a tab.
      if (hash) {
        tab = hash.replace('#', '');
        tabOpener(tab);
      }
    
      // On click, we'll check to see if the event target has a tab hash.
      $('body').on('click', function(e) {
        if (e.target.hash && !e.target.hash.includes('#tab')) {
          tab = e.target.hash.replace('#', '');
          tabOpener(tab);
        }
      });
    });
    • This reply was modified 1 year, 6 months ago by johnwandler.
Viewing 1 replies (of 1 total)
  • The topic ‘Custom link stops working after page refresh or after opening in new tab’ is closed to new replies.