1 changed files with 18 additions and 18 deletions
@ -1,35 +1,35 @@ |
|||||||
document.addEventListener('DOMContentLoaded', function() { |
document.addEventListener('DOMContentLoaded', function() { |
||||||
const urlParams = new URLSearchParams(window.location.search); |
const urlParams = new URLSearchParams(window.location.search) |
||||||
let tab = urlParams.get('tab'); |
let tab = urlParams.get('tab') |
||||||
|
|
||||||
if (!tab && window.location.hash) { |
if (!tab && window.location.hash) { |
||||||
tab = window.location.hash.substring(1); |
tab = window.location.hash.substring(1) |
||||||
urlParams.set('tab', tab); |
urlParams.set('tab', tab) |
||||||
history.replaceState(null, null, `${window.location.pathname}?${urlParams.toString()}`); |
history.replaceState(null, null, `${window.location.pathname}?${urlParams.toString()}`) |
||||||
} |
} |
||||||
function activateTab(tabId) { |
function activateTab(tabId) { |
||||||
const tabElement = document.getElementById(tabId); |
const tabElement = document.getElementById(tabId) |
||||||
if (tabElement) { |
if (tabElement) { |
||||||
tabElement.click(); |
tabElement.click() |
||||||
} |
} |
||||||
history.replaceState(null, null, `${window.location.pathname}?${urlParams.toString()}`); |
history.replaceState(null, null, `${window.location.pathname}?${urlParams.toString()}`) |
||||||
} |
} |
||||||
|
|
||||||
function handleTabClick(event) { |
function handleTabClick(event) { |
||||||
event.preventDefault(); |
event.preventDefault() |
||||||
const targetId = this.getAttribute('href').substring(1); |
const targetId = this.getAttribute('href').substring(1) |
||||||
activateTab(targetId); |
activateTab(targetId) |
||||||
urlParams.set('tab', targetId); |
urlParams.set('tab', targetId) |
||||||
history.replaceState(null, null, `${window.location.pathname}?${urlParams.toString()}`); |
history.replaceState(null, null, `${window.location.pathname}?${urlParams.toString()}`) |
||||||
} |
} |
||||||
|
|
||||||
if (tab) { |
if (tab) { |
||||||
activateTab(`tab_${tab}`); |
activateTab(`tab_${tab}`) |
||||||
} |
} |
||||||
|
|
||||||
window.scrollTo(0, 0); |
window.scrollTo(0, 0) |
||||||
|
|
||||||
document.querySelectorAll('.govuk-tabs__tab').forEach(tabElement => { |
document.querySelectorAll('.govuk-tabs__tab').forEach(tabElement => { |
||||||
tabElement.addEventListener('click', handleTabClick); |
tabElement.addEventListener('click', handleTabClick) |
||||||
}); |
}) |
||||||
}); |
}) |
||||||
|
|||||||
Loading…
Reference in new issue