Growstuff/growstuff

View on GitHub
app/assets/javascripts/tab_navigation.js

Summary

Maintainability
A
0 mins
Test Coverage
$(document).ready(function() {
    var url = location.href.replace(/\/$/, '');

    if (location.hash) {
        var hash = url.split('#');
        var triggerEl = document.querySelector('#myTab a[href="#' + hash[1] + '"]');
        var tab = new bootstrap.Tab(triggerEl);
        tab.show();
        url = location.href.replace(/\/#/, '#');
        history.replaceState(null, null, url);
        setTimeout(function() {
            $(window).scrollTop(0);
        }, 20);
    }

    $('a[data-bs-toggle="tab"]').on('click', function() {
        var newUrl;
        var hash = $(this).attr('href');
        newUrl = url.split('#')[0] + hash;
        history.replaceState(null, null, newUrl);
    });
});