Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Remember which tab was active after refresh

I'm using jquery tabs on a web page and when the page is refreshed it loses what ever tab I had been on and goes back to the first tab.

Has anyone come across this problem and know how to solve it?

like image 547
thegunner Avatar asked Nov 28 '10 22:11

thegunner


People also ask

How do you keep the current tab active on page reload in?

Answer: Use the HTML5 localStorage Object In Bootstrap, if you refresh the page the tab is reset to default setting. However, you can use the HTML5 localStorage object to save some parameter for the current tab locally in the browser and get it back to make the last active tab selected on page reload.

How do you keep the current tab active on page reload in Angularjs?

Set it on selecting tab. To get boolean value of active state for current tab use ng-init . $scope. setActiveTab = function( activeTab ){ localStorage.

How do you make a tab active by default?

Just add the class to your html. This will make the first tab active by default.


2 Answers

Like others I was struggling with my ui-tabs cookie history in jQueryUI 1.10. Thanks to Harry's solution and some other online documentation I refer to in the code below I now have a working non-cookie solution! I was able to test in Firefox 18.0.1 and IE 9.0.12. According to my resources, Chrome, Firefox, Safari and IE8 & above support Session Storage.

  $(function() {     //  jQueryUI 1.10 and HTML5 ready     //      http://jqueryui.com/upgrade-guide/1.10/#removed-cookie-option      //  Documentation     //      http://api.jqueryui.com/tabs/#option-active     //      http://api.jqueryui.com/tabs/#event-activate     //      http://balaarjunan.wordpress.com/2010/11/10/html5-session-storage-key-things-to-consider/     //     //  Define friendly index name     var index = 'key';     //  Define friendly data store name     var dataStore = window.sessionStorage;     //  Start magic!     try {         // getter: Fetch previous value         var oldIndex = dataStore.getItem(index);     } catch(e) {         // getter: Always default to first tab in error state         var oldIndex = 0;     }     $('#tabs').tabs({         // The zero-based index of the panel that is active (open)         active : oldIndex,         // Triggered after a tab has been activated         activate : function( event, ui ){             //  Get future value             var newIndex = ui.newTab.parent().children().index(ui.newTab);             //  Set future value             dataStore.setItem( index, newIndex )          }     });      });  
like image 56
Arik Avatar answered Sep 18 '22 13:09

Arik


I assume that you are using jQuery UI tabs ,

here is an example of using tabs + cookies to save the last clicked tab

http://jqueryui.com/demos/tabs/#cookie

demo : open this link http://jqueryui.com/demos/tabs/cookie.html

the close it and re open it and you will see the same clicked tab

update: after 3 years of this answer jquery ui has deprecated the cookie option : http://jqueryui.com/upgrade-guide/1.9/#deprecated-cookie-option.

but you can still append take a look here if this fits your needs or not https://stackoverflow.com/a/14313315/109217

like image 22
tawfekov Avatar answered Sep 21 '22 13:09

tawfekov