Cookies and javascript (tabs)

I have navigation tabs, and when the user clicks the tab, the div changes using ajax. I would like him to remember which tab the user was on when the user changes the page. I havent made a navigation tab, and I'm brand new to javascript / jquery. Here is the javascript for the tabs:

 jQuery('#contentContainer #tabNavi .nav-item').each(function(i, item) {
        jQuery(item).bind('click', function() {
            if (jQuery('a', this).hasClass('activeTab')) {
                return;
            } else {
                jQuery('#contentContainer #tabNavi .nav-item' a').removeClass('activeTab').eq(i).addClass('activeTab');
                channel_id = jQuery('a', this).attr('href').split('#')[1];
                if (channel_id == _channel) {
                    return;
                }

            }
        })
    });

Navigation links are as follows:

<li><a href="#39">Link1</a></li>
<li><a href="#53">Link2</a></li

Now I have the href value stored in the cookie, but I don’t know how I can change the active class to the right li element when the user comes to the page and he was there before and he clicked on some tab.

+3
source share
1 answer

(jQuery(item).bind('click', function() {...})

var selectedTab = $.cookie('selectedTab');

if (selectedTab) {
    $('li[href="' + selectedTab + '"]').click();
}

UPD

(function($) {

    $('#contentContainer #tabNavi .nav-item a').click(function() {

        var $link = $(this);
        $link.click(function() {
            if (!$link.hasClass('activeTab')) {
                $('#contentContainer #tabNavi .nav-item a.activeTab').removeClass('activeTab');
                $link.addClass('activeTab');
                $.cookie('selected-tab', $link.attr('href'));
            }

            return false;
        });

    });

    var selectedTab = $.cookie('selected-tab');
    if (selectedTab) {
        $('#contentContainer #tabNavi .nav-item a[href="' + selectedTab + '"]').click();
    }

})(jQuery);
+2

Source: https://habr.com/ru/post/1764563/


All Articles