Last active
March 23, 2023 13:48
-
-
Save marcus-at-localhost/84006f66aa65b08cf7652fc547998ec2 to your computer and use it in GitHub Desktop.
[Save Bootstrap 5 Tab State in URL for Deep Linking] #bootstrap #js <https://codepen.io/localhorst/pen/dyqbboY>
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//@see https://codepen.io/localhorst/pen/dyqbboY | |
(function () { | |
'use strict' | |
if(location.hash){ | |
const triggerEl = document.querySelector(location.hash) | |
bootstrap.Tab.getOrCreateInstance(triggerEl).show() // Select tab by name | |
} | |
const selectableTabList = [].slice.call(document.querySelectorAll('[data-bs-toggle="tab"]')) | |
selectableTabList.forEach((selectableTab) => { | |
selectableTab.addEventListener('shown.bs.tab', event => { | |
console.log('Previous URL: ' + location.href); | |
history.replaceState(null, null, '#' + event.target.id); | |
}) | |
}); | |
})(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment