Skip to content

Instantly share code, notes, and snippets.

@marcus-at-localhost
Last active March 23, 2023 13:48
Show Gist options
  • Save marcus-at-localhost/84006f66aa65b08cf7652fc547998ec2 to your computer and use it in GitHub Desktop.
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>
//@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