Binding to Bootstrap Tabs for reloading a page

Wed, 13 June 2018

jquery.bsTabBind.js
jQuery.extend({
    bsTabBind: function(tab_identifier) {
        $(tab_identifier + ' a').click(function(e) {
            e.preventDefault();
            $(this).tab('show');
        });

        $('a[data-toggle="tab"]').on("shown.bs.tab", function(e) {
            var id = $(e.target).attr("href").substr(1);
            window.location.hash = 'btb-' + id;
        });
        var hash = window.location.hash;
        if (hash.indexOf('btb-') === 1) {
            hash = hash.substr(5);
            $(tab_identifier + ' a[href="#' + hash + '"]').tab('show');
        }
    }
});
usage.html
    <nav>
        <div class="nav nav-tabs" id="tabControl1" role="tablist">
            <a class="nav-item nav-link active" id="one-tab" data-toggle="tab" href="#tab-one" role="tab" aria-controls="tab-one" aria-selected="true">Tab 1</a>
            <a class="nav-item nav-link" id="two-tab" data-toggle="tab" href="#tab-two" role="tab" aria-controls="tab-two" aria-selected="false">Tab 2</a>
        </div>
    </nav>
    <div class="tab-content" id="nav-tabContent">
        <div class="tab-pane fade show active" id="tab-one" role="tabpanel" aria-labelledby="one-tab">
            tab 1
        </div>
        <div class="tab-pane fade" id="tab-two" role="tabpanel" aria-labelledby="two-tab">
            tab 2
        </div>
    </div>

    <script>
    jQuery.bsTabBind('tabControl1');
    </script>