Tab-Navigation - Remember user selection

Give id for each tab.Then ,

$(".icon-cross").click(function(){
    var selected_id= $(this).attr('id);
    //save this id to cookie.Then retrieve this cookie value on next page visit
})

You can use sessionStorage to store (while the user's browser is open) the last used tab and retrieve each time the page loads:

$(document).load(function(){
   if (sessionStorage.getItem('lastsessionid') != null){
       //go to anchor, simulates the link click
       $(document).scrollTop( $(sessionStorage.getItem('lastsessionid')).offset().top );
   }
   $('a[href=#section*]').on('click', function(){
       sessionStorage.setItem('lastsessionid', this.href);
   });
});

EDIT: Woooow, it turned into so highly voted answer, I'll improve it. The main idea of my solution is to not reinvent the wheel about the W3C standards about anchor, not only what W3C expect (some browsers it's way ahead of those standards), but what the programming common sense expect from it. IMHO, the most important thing when you're overcoming an attribute's limitations is to try to continue their natural behavior, avoiding 'magic' features and other piece of codes that will become hard to maintain in the future. What the OP needed was to store some kind of value to use after and just that.

Other approach that would be common it's to use trigger techniques, but I strongly recommend to avoid triggers because you're simulating user's interaction unnecessarily and, sometimes, the application has other routines associated with the element's click event where can turn the use of clicking bothersome. It's true that the way I did the anchor behavior was simulated too, but, well, in the end it's my coding culture to avoid triggers to maintain the nature of event's calls:

$(document).load(function(){
   if (sessionStorage.getItem('lastsessionid') != null){
       //go to anchor, simulates the link click
       $(sessionStorage.getItem('lastsessionid')).trigger('click');
   }
   $('a[href=#section*]').on('click', function(){
       sessionStorage.setItem('lastsessionid', this.href);
   });
});

To some kind of information, in the old times the web devs (I'm not so old, but I programmed before the javascript turned in so great tool into web dev. routine) couldn't rely so much on scripting techniques, so what they do to propagate some kind of behavior has store that anchor into some post/get variable and perpetuate that value until the user reaches the desired routine, forcing way through the link to load with an #{$desiredAnchor} to force the anchor behavior. Obviously it is a method that has its flaws and blind spots, but was very common when the javascript was not the big role into the industry. Some devs can need that idea when working in such small pads from department store, per example, where the pad's browsers is too old to work well with javascript ES4/ES5/ES6 new functions.


The problem is normal libraries for tabs do not preserve the hash of the link to the tab. So, your browser is unable to track the progress of tabs and hence the back button does not work in case of tabs as you expected.

So the idea is to preserve # link in href tab. If one is able to preserve that then browser will be able to track users progress through tabs and back button will land user on the previous tab.

You can store the # value in hash property of window.location.

See the answer below

Bootstrap 3: Keep selected tab on page refresh

This might have a solution for you.

After, you have preserved the hash and now able to work out the back button of the browser handle your transitions by capturing the onhashchange event of the javaScript.

Here, you can manage to change tabs when user hits back in the browser.

Hope this would help.

Tags:

Html

Php