document.addEventListener("DOMContentLoaded", function() {
// 1. Get your two top tabs by their IDs
const tab1 = document.getElementById('top-tab-1');
const tab2 = document.getElementById('top-tab-2');
// 2. Track when a user manually clicks a top tab to save their preference
if (tab1) {
tab1.addEventListener('click', function() {
sessionStorage.setItem('preferredTab', 'tab1');
});
}
if (tab2) {
tab2.addEventListener('click', function() {
sessionStorage.setItem('preferredTab', 'tab2');
});
}
// 3. Check the current page URL
const currentUrl = window.location.href.split('?')[0].replace(/\/$/, "");
let tabToHighlight = sessionStorage.getItem('preferredTab');
// 4. Hardcoded rules for your shared link vs unique links
if (currentUrl.includes('/advanced-engineering-exhibitors')) {
// This is your shared link. It relies on the saved memory.
// If there is no memory yet (direct visit), default to tab1.
if (!tabToHighlight) {
tabToHighlight = 'tab1';
}
} else {
// For all other pages, determine the tab based on WordPress menu classes
const matchingLink = document.querySelector(`.main-navigation a[href*="${window.location.pathname}"]`);
if (matchingLink) {
if (matchingLink.classList.contains('link-tab-1')) {
tabToHighlight = 'tab1';
} else if (matchingLink.classList.contains('link-tab-2')) {
tabToHighlight = 'tab2';
}
}
}
// 5. Save the decision to memory for the next page load
if (tabToHighlight) {
sessionStorage.setItem('preferredTab', tabToHighlight);
}
// 6. Apply your active/highlight visual CSS class
if (tabToHighlight === 'tab1' && tab1) {
tab1.classList.add('tab-active');
} else if (tabToHighlight === 'tab2' && tab2) {
tab2.classList.add('tab-active');
}
});