Custom link stops working after page refresh or after opening in new tab

[ad_1]

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 site will teach you how to build a WordPress website for beginners. We will cover everything from installing WordPress to adding pages, posts, and images to your site. You will learn how to customize your site with themes and plugins, as well as how to market your site online.

Buy WordPress Transfer