diff --git a/tabHashEvent.js b/tabHashEvent.js index e967da4..eff4d89 100644 --- a/tabHashEvent.js +++ b/tabHashEvent.js @@ -9,15 +9,34 @@ $('div.newsContainerDiv > a[href^="#"]').on('click', function (e) { $('li').removeClass('active'); $('a[href="' + targetTabId + '"]').parent().addClass('active') + // This prevents the page from scrolling down to where it was previously. + if ('scrollRestoration' in history) { + history.scrollRestoration = 'manual'; + } + // This is needed if the user scrolls down during page load and you want to make sure the page is scrolled to the top once it's fully loaded. This has Cross-browser support. + window.scrollTo(0,0); + }); -$('a[data-toggle="tab"]').on('shown.bs.tab', function (e) { - var target = $(e.target).attr("href") // activated tab - window.location.hash = $(e.target).attr('href'); - // console.log(target); +$(document).ready(function() { + $('a[data-toggle="tab"]').on('show.bs.tab', function(e) { + const activeTabId = $(e.target).attr('href'); + localStorage.setItem('activeTab', activeTabId); + }); + + var activeTabStore = localStorage.getItem('activeTab'); + if (activeTabStore) { + $(activeTabStore).tab('show'); + } }); +// old code that will have the hash in the url but somehow casuing the page to scroll down automatcally +// $('a[data-toggle="tab"]').on('shown.bs.tab', function (e) { +// var target = $(e.target).attr("href") // activated tab +// window.location.hash = $(e.target).attr('href'); +// // console.log(target); +// });